简体   繁体   中英

UIView size/position not working on iPad as on iPhone

I have a UIView of size width=320px height=40px which I want to display at the bottom looking like a tab-bar.

I use the following code to accomplish this and it works fine on iPhone. But when I try the iPad simulator, the view does not appear anywhere on the screen.

buttonsView.frame=CGRectMake(
                                0,
                                self.view.frame.size.height - buttonsView.frame.size.height,
                                self.view.frame.size.width,
                                buttonsView.frame.size.height );

This stretches the view to the width of the screen but keeps it's original height.

How can I make this show right on my iPad also?

[UPDATE]

This happens in my viewDidLoad .

I have this NSLog now:

NSLog(@"buttonsView Width: \t%g\t%g\tHeight:\t%g\t%g",self.view.frame.size.width, buttonsView.frame.size.width, self.view.frame.size.height, buttonsView.frame.size.height);

With output:
iPhone:

buttonsView Width: 320 320 Height: 460 40

iPad:

buttonsView Width: 768 768 Height: 1004 40

I also create another view like this for the top of the page which works fine:

dataView.frame=CGRectMake(
                             0,
                             0,
                             self.view.frame.size.width,
                             dataView.frame.size.height);

Have you looked (using breakpoints or NSLog ) at the values you are putting into the CGRectMake call, just as a sort of "sanity check"?


Thanks for the info. My next thought is that between viewDidLoad and viewWillAppear something is being changed by the OS. Perhaps try moving the code to viewWillAppear so that you are guaranteed the frames you are working with will be the frames that will be displayed. This Stack Overflow answer explains it in a little bit more depth.

This happened because the Auto Resizing was set for the UIView's.

To fix, set the Auto Resizing to bind it to the top left corner. This way whenever you change the frame, it is relative to the top left pixel.

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