简体   繁体   中英

UIView fine for iPhone 4in, but squashed in iPhone 3.5in

I started a new project in the new Xcode, and I see that my default the main window is set to 320x548. I'm fine with that, but when I test my app, a view I added to my main xib, which is supposed to be 280x280, looks more like 280x200 when testing on 3.5in devices.

I've tried changing the settings for that view in the xib, but nothing seems to affect it.

How do I ensure background compatibility so that in older devices the view is the same size?

Update:

When I add the the view programmatically, it all works fine, but when I add it via an outlet in my controller, it all gets squashed. I tried to force (programmatically) the view to be 280x280, but then it just distorts the content of the view. It just wants to be 280x192...

I must be doing something wrong, but I don't know what.

I had the same issue nowadays, and fortunately found the solution. I created a simple application which contains a simple button, I wanted to set it's size with a 10px margin around. The self.view.bounds returns with the actual size of the xib, if it set to for example iPhone4 retina and you test it in a different iPhone version, it will return incorrect value. Use [[UIScreen mainScreen] bounds]; instead, and substract the height of the statusbar ( [[UIApplication sharedApplication] statusBarFrame].size.height ). The final stuff looks like this:

CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.width -= 20;
frame.size.height -= (20 + [[UIApplication sharedApplication] statusBarFrame].size.height);

button.frame = frame;

I tested it on all of the device types, and it worked fine.

(Obviously it only works when the "Use Autolayout" is unticked)

Hope this helps.

Sounds like you need to review the auto sizing constraints that you've set on the view in the .xib file. You can constrain the view to a fixed size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM