Wrong view size on 3.5 screen - iphone

When running my iOS app on a 3.5 inch iPhone Simulator, self.view.frame.size.height is reporting 548px. What is the right way to fix this?

I assume you see this behavior in viewDidLoad, right?
The screen dimensions are not final while in viewDidLoad. You should only rely on the view's frame in viewWillLayoutSubviews method.

it's because you're calling this from viewDidLoad right?
I bet calling the NSLog in this function instead comes back with the correct height;
-(void)viewDidAppear:(BOOL)animated {
NSLog(#"%f", self.view.frame.size.height);
}
You should really be using self.view.bounds instead of self.view.frame
for more information about why and how view this SO:
"Incorrect" frame / window size after re-orientation in iPhone

Related

How a keyboard extension know it's running in a iPhone app on iPad?

When using iPhone app on a iPad, the keyboard extension can only know the screen size of an iPad, but actually I should know it's like running on an iPhone, and get a size like an iPhone.
My current code runs in a iPhone app on iPad look like this:
In viewDidAppear of UIInputViewController, I can actually get the frame of self.inputView, that is 320 in this case.
However, self.view and self.inputView's frame are both CGRecteZero in viewDidload or viewWillAppear, and that's actually why we should set keyboard height only after [super viewDidAppear].
The actual size of the keyboard view(self.view.frame) can be checked right after invoking [super viewDidLayoutSubviews] in viewDidLayoutSubviews of UIInputViewController. It is the 1st place to check the size and much faster than viewDidAppear.
Your Keyboard extension should be universal, if so then you can get device type (iPhone/iPad) by using below code.
let isPad = UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
It may possible that you are using below method to get screen bounds
UIScreen.mainScreen().bounds.size
This method gives device's whole height, width in keyboard extension.
You can use nested condition by using above two condition to solve your problem.

Application Screen distorted after iOS upgrade

The application which runs smoothly on 4.0.1 when was tried on run on 4.2.1 produced distorted screen i.e., screen somewhat moved to left by 20%. Phones on which iOS 4.0.1 and 4.2.1 are installed are 2 different phones. What could be the problem?
We observed that wherever we have added as subview this problem is occurring.
Thanks,
Satish
From your description, I think it is either that you are not setting the correct view.autoresizingMask properly or there is a subtle change in how views are being laid out.
Try setting the frame of the view that moved explicitly and see what happens.
[view setFrame:CGRectMake(0, 0, 320, 460)]
Some related piece of code and/or screenshots would definitely help. Also, is it a UIKit application or a Cocos2D game?
EDIT: Since you can't provide code (or a stripped-down example version) I'll just post some code that I've been using.
Usually when I add subviews to "fill" a parent view, I had do the following:
UIView *parent = nil; // find parent view
UIView *child = nil; // child view to add to parent
[parent addSubview:child];
CGRect frame = [parent frame];
frame.origin.x = 0;
frame.origin.y = 0;
[child setFrame:frame];
If you have done that and it still does not work I believe the problem might lies somewhere else (i.e. have you overridden layoutSubviews by any chance? or was the phone jailbroken?)
I highly doubt it'll be a UIKit bug though but it is totally possible.

Landscape/Portrait conflict - iOS

Just when i thought I had everything figured out .. i got this problem.
the scenario.
I got a simple tableView. and with a search bar in navigation item's titleView. The SearchBar is added to navItems titleView via a uibarbuttonitem in view controllers toolbar.
NOW, normally
After initiating the searchbar with [beginResponder] the keyboard shows up. And It sends out a notification "KeyboardDidShow" which is where i calculate the UIKeyboard's height and set the tableView's height accordingly (Shorten it).
ON Rotation - to and fro landscape/portrait, everything works fine.
-(void)didRotateInferfaceOrientation is called and everythings kool.
Heres the problem.
When the keyboard is active, it has a Google "search" button, this pushes to a new view - webviewcontroller.
the problem is, this
When, [PORTRAIT]ViewController [SearchBar with keyboard active] --> taps Search --> [Portrait]WebViewController --> Change Device Orientation to [Landscape] --> [Landscape]WebViewController changes to landscape ---> HERES THE PROBLEM, user taps back to uiViewController[Landscape]
the method -didRotatefromInterfaceOrientation isnt called. and somehow the tableView height is messed up. Though the view is rotating perfectly.
Is there something im missing here..
would appreciate any help. .thanks
When user taps back, -didRotatefromInterfaceOrientation will not be called. You need to check orientation in viewWillAppear (or call viewDidLoad, prior to returning from tap on back), and then call the proper layout for the chosen orientation.
In all of your (BOOL)shouldRotate... methods, you should be call a separate method to ensure your layout is correct for the device orientation.
I got a similar problem in one of my applications recently, not exactly our problem but don't bother, you should see what I'm heading for: I wanted to simply rotate an viewController displayed using presentModalViewController...Unfortunatly it didn't really worked put, especially on old iPhone with OS prior to iOS 4...So I needed to rotate programatically! Just get your screen size, use CGAffineTransform or something like that and change the sizes and then you should be done...
If your interested I could post a bunch of code, so let me know!
EDIT:
UIScreen *screen = [UIScreen mainScreen];
myController.view.bounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - 20);
if(currentOrientation == UIDeviceOrientationLandscapeRight){
myController.view.transform = CGAffineTransformConcat(myController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(-90)));
}else{
myController.view.transform = CGAffineTransformConcat(myController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90)));
}
myController.view.center = window.center;
[[UIApplication sharedApplication] setStatusBarOrientation:currentOrientation];
[self.window addSubview:self.tabBarController.view];
[self.window bringSubviewToFront:self.tabBarController.view];
[self.window addSubview:myController.view];
[self.window bringSubviewToFront:myController.view];
[self.tabBarController.view removeFromSuperview];`
This also includes removing a TabBar when rotating to landscape to get some more space...enjoy :)
You could call didRotateFromInterfaceOrientation manually on viewWillAppear and just pass an orientation yourself (i.e. [UIApplication sharedApplication].statusBarOrientation).

How do I detect orientation on app launch for splash screen animation on iPad!

Hi I have an app and I have two *.pngs for default splash screen:
Default-Landscape.png
Default-Portrait.png
What I want is to animate this default splash screen away when my app is loaded and ready to go.
To achieve this I would normally present an UIImageView with either default-landscape or default-portrait (depending on the device orientation), keep it on screen for a certain time and then animate it away.
My problem is that if I call [[UIDevice currentDevice] orientation] in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
The answer is always that the device is in portrait orientation even if I clearly have it in landscape. I tried this in simulator and on the device as well and the behaviour is the same.
Does anyone know a fix for this or maybe some other approach?
Thanks!
I had troubles with this and I solved it by making one image 1024x1024 and setting the contentMode of the UIImageView to UIViewContentModeTop, then using left and right margin autoresizing. So long as your portrait and landscape default images are the same layout then this will work fine.
Just to clarify here's what I used:
bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SplashImage]];
bgView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
bgView.contentMode = UIViewContentModeTop;
To get around this problem I installed the splash image view inside of a view controller that allowed both orientations. Even though the device reported the wrong orientation at startup, the view controller seems to get the right one.
You can use UIApplication statusBarOrientation as follows:
if ( UIDeviceOrientationIsLandscape( [[UIApplication sharedApplication] statusBarOrientation] ))
{
// landscape code
}
else
{
// portrait code
}
Maybe you could show a blank view with black background at start time and place [[UIDevice currentDevice] orientation] into this view's viewDidAppear and start your splash screen from there?
Another solution would be to read the accelerometer data and determine the orientation yourself.
To know at start what is the orientation (UIDevice orientation don't work until user have rotate the device) intercept shouldAutorotateToInterfaceOrientation of your View Controller, it is called at start, and you know your device orientation.
There are certainly times when you want to transition from the loading image to something else before the user gets control of your app. Unless your app is really simple, going from loading image to landing page probably won't be sufficient without making the app experience really suck. If you ever develop a large app, you'll definitely want to do that to show progress during setup, loading xibs, etc. If an app takes several seconds to prepare with no feedback, users will hate it. IMO, there's nothing wrong with a nice transition effect either. Almost nobody uses loading screens the way Apple suggests to. I don't know which apps you looked at that showed the "empty UI" type loading screens they suggest. Heck, even Apple doesn't do that except in their sample code and none of my clients would find that acceptable. It's a lame design.
Only the first view added to the window is rotated by the OS. So if you want your splash screen to automatically rotate AND your main view is rotatable then just add it as a child of that view.
Here is my code for fading out the appropriate splash screen image:
// Determine which launch image file
NSString * launchImageName = #"Default.png";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
launchImageName = #"Default-Portrait.png";
} else {
launchImageName = #"Default-Landscape.png";
}
}
// Create a fade out effect
UIImageView* whiteoutView = [[[UIImageView alloc] initWithFrame:self.window.frame] autorelease];
whiteoutView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
whiteoutView.autoresizesSubviews = YES;
whiteoutView.image = [UIImage imageNamed:launchImageName];
[[[self.window subviews] objectAtIndex:0] addSubview:whiteoutView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
whiteoutView.alpha = 0.0;
[UIView commitAnimations];
Note: You'll have to update it to support hi-res screens.
It sounds like you're not using the launch image the way Apple recommends in the iOS HIG. It specifically calls out that you should not use it as a splash screen, but rather as a skeleton version of your actual UI. The net effect is that your app appears to be ready just that much faster.
The suggestions that you could draw a splash screen yourself after the app has launching in viewDidAppear or similar also are missing the basic purpose of a launch image. It's not a splash screen. If your app is ready, let the user interact with it, don't waste their time drawing a splash screen.
From my five minute survey of Apple apps and third-party apps, everyone showed a portrait launch image, loaded the portrait version of the UI, and then rotated to landscape. It's been a while since programming on iOS, but I think this mirrors the order of the method calls -- first your app gets launched, then it is told to rotate to a particular orientation.
It might be a nice enhancement request to file with Apple though :)

iPad touch detected only in 320x480 region

I'm upgrading iPhone app and so far everything is going well (managed to resize screen display for most forms). However, I can't solve one problem - the touch is detected only in old, 320x480 region. Any ideas how can I solve that?
Thanks
EDIT: Here are the results when code for fetching superview bounds executes:
CGFloat wdth = self.view.superview.bounds.size.width;
NSLog(#"%d", wdth);
CGFloat hgth = self.view.superview.bounds.size.height;
NSLog(#"%d", hgth);
Result:
2010-04-16 14:25:36.268 xxx[14871:207] 0
2010-04-16 14:25:36.269 xxx[14871:207] 1073741824
Result for (%f):
2010-04-16 14:37:26.048 xxx[15053:207] -1.998374
2010-04-16 14:37:26.049 xxx[15053:207] 0.000000
Result for (%g):
2010-04-16 14:37:41.113 xxx[15084:207] -1.99837
2010-04-16 14:37:41.115 xxx[15084:207] 9.48221e-38
http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html
In you AppDelegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// stupid Apple...
CGRect rect = [[UIScreen mainScreen] bounds];
[window setFrame:rect];
// now, display your app
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
In interface builder make sure that mainwindow-iPad "Full screen at launch" window attribute is checked.
I have seen similar behavior when a container view is set too small. The subviews will display fine but touches will not fire because the touches are happening outside of the superview.
The real problem here is that in the file MainWindow-iPad.xib, the Window object gets created with a size of 320 x 480. As ayman2010 mentions, setting the "Full screen at launch" fixes this.
Why would apple create an iPad window so small!!!???
You may also open the (automatically created) MainWindow-iPad.xib in Interface Builder, select File/Create iPad Version, close the MainWindow-iPad.xib and save the unnamed new XIB (which has the correct size) over the old one.
BurninLeo