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
}
Related
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.
I'm developing a universal app.
When my device is ipad I have 2 designs: Portrait and landscape.
I use this method: -(void)orientationDidChanged:(NSNotification *)notification
It works perfectly when I rotated my device. But I have a problem, when first load the view and I have not rotated the device.
For that reason, I put this code in the viewdidload:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ //
something.
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
NSLog(#"IPAD ** ");
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
NSLog(#" vertical ** ");
}
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
NSLog(#"Horizontal ** ");
}
}
My problems is this: sometimes it works , sometimes not :(. This method: orientationDidChanged, when start to work? only if I rotate my device or immediately with the viewDidLoad , is necessary ask one more time the orientation when I have that method? Thanks for some advice.
I don't have problem when I rotated my device, it works perfectly :D .. My problem is when I recently run the application and I haven't turned the device
You can use this:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) //Default orientation
//UI is in Default orientation (Portrait)
else if(orientation == UIInterfaceOrientationPortrait)
//UI is in Portrait orientation
else if(orientation == UIInterfaceOrientationLandscapeLeft)
//UI is in Landscape-left orientation
else if(orientation == UIInterfaceOrientationLandscapeRight)
//UI is in Landscape-right orientation
I have a UITabBarController with two UITableViews, all were created in the storyboard.
The problem is that in the second tableview the first few lines of the table are under the top bar, this doesn't happens with the first tableview, even if I change the order of the views the first will work perfectly and the second will present the problem, so the one that was working perfectly now presents the same problem because is the second item of the tabbar controller.
I don't have much code to show because I didn't create the tableviews programatically.
Not sure exactly based on your description, but a couple possibilities come to mind:
Check your view controller attributes inspector in IB. Look for
"Extend Edges" option under View Controller, and uncheck "Under Top
Bars" if it is checked.
In ios7, there appears to be a behavior in UIScrollView and all
subclasses whereby the content inset and offset is automatically adjusted, sometimes not very well. You
can try disabling that either in code or IB (see link for
how: iOS 7 -- navigationController is setting the contentInset and ContentOffset of my UIScrollView)
For any newly created iOS >7.0 app I suggest you take a deeper look at autolayout. For all my old iOS 6 apps I solved it like this:
In your UITableViewController interface:
bool _hasStatusBar;
bool _hasStatusBarHeight;
UIView *_blendView;
In your UITableViewController implementation file:
-(void)viewWillAppear:(BOOL)animated{
_hasStatusBar = NO;
_blendView = nil;
[self.tableView reloadData];
}
-(void)viewWillDisappear:(BOOL)animated{
_hasStatusBar = NO;
_blendView = nil;
}
- (void) viewDidLayoutSubviews {
// WTF APPLE!?
if (!_hasStatusBar) {
int topBarOffset = 20;
_hasStatusBar = YES;
// Fix for iOS 7 overlaying status bar
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect viewBounds = self.view.bounds;
_blendView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewBounds.size.width, topBarOffset)];
[_blendView setBackgroundColor:COLOR_MAIN];
[_blendView setOpaque:YES];
[_blendView setAlpha:1.00];
UIView *whityMacWhite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewBounds.size.width, topBarOffset)];
[whityMacWhite setBackgroundColor:[UIColor whiteColor]];
[whityMacWhite setOpaque:NO];
[whityMacWhite setAlpha:0.80];
[_blendView addSubview:whityMacWhite];
[self.view.superview addSubview:_blendView];
}
}
}
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.
Okay, I've got my normal app which is in portrait mode. I can force my app to go to landscape mode for a view (using navigationcontroller and viewcontroller) like this:
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
But then when I go back to the main menu (tableview) it goes straight back to portrait. I try this code:
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
But that doesn't work..
Any ideas?
Take a look at the function shouldAutorotateToInterfaceOrientation: in the UIViewController class. This function returns YES if the orientations are supported by your UIView. If you return YES only to the landscape orientation, the iPhone will automatically be put in that orientation.
The following code should do it. Put it in the UIViewController that controls the view that you want to put in landscape mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscape);
}
To get this to work, I added this to the rootviewcontroller:
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
This seems to be working now.
Here's what I'm doing to do this:
first, put this define at the top of your file, right under your #imports:
#define degreesToRadian(x) (M_PI * (x) / 180.0)
then, in the viewWillAppear: method
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
if you want that to be animated, then you can wrap the whole thing in an animation block, like so:
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];
Then, in your portrait mode controller, you can do the reverse - check to see if its currently in landscape, and if so, rotate it back to Portrait.
You need to edit your Info.plist file to add the UIInterfaceOrientation key with the appropriate value (UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft).
You can't use this private API.
There is a solution to do that : it's to use a view controller and adding its view to the window. then in that controller you force landscape in the shouldAutorotate... methode. It works fine, but be sure it's necessary for your project to use that, because it's not very smart to force the user to turn his iPhone. By the way, here is an example code if you need it.
http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html