change a buttons frame size when another button tapped Xcode - uibutton

I have an app that I want a button's frame size to change on different devices when another button is tapped. When I tap the button on the iPad the other button's frame size changes and changes back when another button is tapped. However this does not happen when on an iPhone.
this is the original size on the iphone
(568 height)
button.frame = CGRectMake(18,456,126,13);
other height
button.frame = CGRectMake(18,370,126,13);
and this is the size on the iPad
button.frame = CGRectMake(47,577,251,26);
this is the code to change the size of the button when the other button is tapped
-(IBAction)changeSize:(id)sender{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 568) {
self.button.frame = CGRectMake(18,339,100,13);
}
else {
self.button.frame = CGRectMake(18,279,100,13);
}
}
else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
self.button.frame = CGRectMake(47,603,200,26);
}
Like I said it works fine changing the button size on the iPad but not on the iPhone.
Any suggestions?
Thanks

So I had auto layout enable for the iPhone and not for the iPad. Once I disabled it on the iPhone it worked perfectly.

Related

UIScrollview content size when orientation changes

I have a scrollview with pagination. In viewDidLoad i check if the current orientation is landscape then i set its contentsize's height 440
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];
}
else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
{
[scroll setFrame:CGRectMake(0,0,480,480)];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];
}
everything works fine scrollview scrolls smoothy and there is no diagonal scrolling.
but when orientation changes,
i have to set scrollview's frame and contentsize again and i am setting it as follow
-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
self.scroll.frame = [[UIScreen mainScreen]bounds];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}
else
{
self.scroll.frame = CGRectMake(0,0,480,480);
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}
}
i cant understand why i have to set content size's height upto 600 in landscape mode, and that too isnt enough. and it adds one more problem that scrollview starts scrolling diagonally which i dont want as it looks so weird. Can anyone help me understanding where and what i am missing?
i have set scrollview's autoresizing mask as
[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
but changing it doesnt help.
Don't use UIDeviceOrientation. Use UIInterfaceOrientation instead. DeviceOrientation has two extra options you don't need here. (UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown)
Return Yes from shouldAutorotateToInterfaceOrientation
Now willRotateToInterfaceOrientation: duration: will be called every time you rotate your device.
Implement this method like this.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect frame;
int pageNumber = 2;
int statusBarHeight = 20;
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
frame = CGRectMake(0, 0, 480, 320 - statusBarHeight);
} else {
frame = CGRectMake(0, 0, 320, 480 - statusBarHeight);
}
scrollView.frame = frame;
scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height);
}
Let,
pageNumber = 2
statusBarHeight = 20
Here it is a problem in your code. Why you are setting the frame size like this?? You have only the screen size of 320px width. And when it changes to landscape, height will be only 320px. But you are setting the scroll height as 480px and it goes out of the screen and start to scroll diagonally.
self.scroll.frame = CGRectMake(0,0,480,480);
Instead of that frame size, change like this
self.scroll.frame = CGRectMake(0,0,480,320);
And you need to set the content size depend on the content you are having inside the scroll view in either orientation.

iPhone 5 Auto Layout not Correct

The new iPhone screen has screwed up my app. On the iPhone 3.5 inch screen, everything is out of place. I have two different views in the same storyboard I have tried out detecting and trying to switch storyboard views but nothing has worked yet. Could I get some code on how to switch to the right view at the launch? Do I place this code in my main view controller, or do I put this in my app delegate?
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
} else {
// code for 3.5-inch screen
}
What my views look like http://www.fileden.com/files/2012/6/18/3317719/screenshot7.jpg
Initialize this in the app delegate for the app in "didFinishLaunchingWithOptions:" and then call the right NIB or Storyboard for the device/screen size. Create a different Storyboard for each and if you wish use auto layout for iOS 6 in your Storyboard. Start your if Statement with something like this:
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
//iPhone 5 storyboard
}
else {
// other storyboard here
}

ipad orientation issue with modelview

I have button in main view,when i click on main view button model view Mv1 is open.Mv1 model view width and height is 730 and 620.in portrait mode Mv1 model view show center in main view but in landscape mode Mv1 model view show center horizontally but does not show center vertically in main view.
main view button click coding:-
Mv1 *Mv1obj = [[Mv1 alloc]initWithNibName:#"Mv1" bundle:nil];
UINavigationController*nav[[UINavigationControlleralloc]initWithRootViewController:Mv1obj];
nav.modalPresentationStyle=UIModalPresentationFormSheet;
nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:nav animated:YES];
nav.view.superview.frame = CGRectMake(0, 0,730,620);
nav.view.superview.center = self.view.center;
if know please replay,thanks in advance.
The issue here is that you need to account for orientation when setting the center, so replace the line:
nav.view.superview.center = self.view.center;
with something like
UIDeviceOrientation orientation = ([[UIDevice currentDevice] orientation]);
const CGPoint centerPortait = self.view.center;
const CGPoint centerLandscape = CGPointMake(centerPortait.y, centerPortait.x);
const CGPoint center = (orientation == UIDeviceOrientationPortrait) ? centerPortait : centerLandscape;
nav.view.superview.center = center;
this will correctly center the modal view.

'willRotateToInterfaceOrientation' not working for my DetailViewController

I'm developing this application on the iPad. My MainWindow contains a Split View Controller which loads the RootViewController and DetailViewController.
I have this image that is placed at the bottom-right of the DetailViewController.
My application allows different orientations, therefore i want the image to appear at different positions for different orientations.
This is my code in DetailViewController:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}
When i launch my application and my iPad is at portrait orientation, the image will be correctly placed at (X:244, Y:518) which is at the bottom right.
But when i launch my application and my iPad is at landscape orientation, the image will not be shown and i suspect that it is still at the position for portrait orientation. Only when i rotate my iPad to portrait orientation and then back to landscape orientation, then the image will appear at (X:183, Y:257) which is correct.
What i tried to do is to place these codes in my DetailViewController's 'viewWillAppear' method:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}
But when i launch my application, it still has the same problem.
What can i do so that when i first launch my application in landscape orientation, it will be correctly placed at (X:244, Y:518) ?
You might want to try putting your resizing logic in didRotateFromInterfaceOrientation instead of willRotateToInterfaceOrientation.
My custom layout code only worked when I called it after the rotation had completed it, rather than before.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
NSLog(#"didRotateFromInterfaceOrientation");
float width;
float height;
if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
width = [[UIScreen mainScreen] applicationFrame].size.width;
height = [[UIScreen mainScreen] applicationFrame].size.height;
NSLog(#"Changing to portrait: %f x %f", width, height);
}
else
{
width = [[UIScreen mainScreen] applicationFrame].size.height;
height = [[UIScreen mainScreen] applicationFrame].size.width;
NSLog(#"Changing to landscape: %f x %f", width, height);
}
NSLog(#"Changed orientation: %f x %f", width, height);
// Call my custom layout function to setFrame on all my UI controls
[self doLayout:width height:height];
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
If your app shows the status bar then you probably need to call self.view.frame.size.width instead of getting the size from the applicationFrame.
Make sure that you're overriding
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; // Override to allow rotation. Default returns YES only for UIInterfaceOrientationPortrait
To return YES for each UIInterfaceOrentation that you want to support, otherwise willRotateToInterfaceOrientation and didRotateToInterfaceOrientation wont get called.
Try calling this early in your applicationDidFinishLauching:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Since the detailViewController wouldn't sometimes know its orientation, I solved this issue by having a shared variable in the appDelegate that would store the device orientation throughout the app. You can initialize it in the splitViewController with its orientation so when the detailViewController loads it can read it from there.

Device orientation at launch

I've read ALL of the posts on this topic, but still can't figure this one out.
I add a VC to the window in applicationDidFinishLaunching. The VC contains a UIImageView.
I want to set the image of the UIImageView based on the device orientation, but statusBarOrientation, deviceOrientation, interfaceOrientation, everything I check returns portrait.
There's gotta be something simple I'm missing.
Your help is appreciated.
Have you enabled the other orientations in your Info.plist? There's a graphical editor for this in Xcode 4's project editor.
Sometimes (not sure if this is in every case, or just the simulator), the app will launch in one orientation and then immediately rotate to another orientation to match the device orientation.
I would suggest implementing -[UIViewController willRotateToInterfaceOrientation:duration:] to set your image, which should be called immediately after.
Well.. it seems there were several issues. 1) The simulator doesn't consistently generate orientation (or change in orientation) data as expected. Also, 2) immediately at start up, the device may (or may not) send statusBarOrientation info in time to read that. So, I did this right in the didFinishLaunching method in the app delegate.
// add the splash IV to the window per the current orientation, then animate it away
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
self.theSplashIV.image = [UIImage imageNamed:#"Default-Portrait~ipad.png"];
self.theSplashIV.frame = CGRectMake(0, 20, 768, 1004);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown){
self.theSplashIV.image = [UIImage imageNamed:#"Default-Portrait~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI);
self.theSplashIV.frame = CGRectMake(0, 0, 768, 1004);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft){
self.theSplashIV.image = [UIImage imageNamed:#"Default-Landscape~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI / 2);
self.theSplashIV.frame = CGRectMake(0, 0, 748, 1024);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight){
self.theSplashIV.image = [UIImage imageNamed:#"Default-Landscape~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI / -2);
self.theSplashIV.frame = CGRectMake(20, 0, 748, 1024);
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
And then I animate away the splash IV with a delay.
Not very elegant, but it works.