How to fix an UIButton/UILabel etc to certain positions in different orientations?
How to fix an UIButton/UILabel etc to certain positions in different orientations? thanks!!! When I set different frame for different orie开发者_C百科ntations strange things happen? is there a solution?
It will be better to create 2 view. One for Portrait and One for Landscape.
Then you can set all the data accordingly in proper format.
Then just make replacement of View according to device orientation.
if((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
[myLandscapeView removeFromSuperview];
[myPortraitView setFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:myPortraitView];
[self.view bringSubviewToFront:myPortraitView];
}
if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
[myPortraitView removeFromSuperview];
[myLandscapeView setFrame:CGRectMake(0, 0, 480, 320)];
[self.view addSubview:myLandscapeView];
[self.view bringSubviewToFront:myLandscapeView];
}
I hope it will be helpful to you.
Cheers.
Making two views is only necessary if your view is very complicated, like the iPhone calculator, which changes completely on rotation. If you just want to re-center buttons or keep a switch in the corner, for example, you'll instead want to use the autoresizing system. This system is sometimes called "struts and springs." There's a nice, simple introduction to it at Techotopia.
Yep! You need layoutSubviews. It will be called whenever the orientation changes, or at first before the view appears.
Use this method to layout the subviews depending on the view's bounds. For example:
- (void)layoutSubviews {
CGRect bounds = self.bounds;
do your layout stuff here
}
Layout is quite a complex subject, and you'll find tons of examples, tutorials out there. The way I usually approach it personally is to consider for each element how you want it positioned for the different orientations. Does the width or height need to change? Should something be fixed relative to the top or left of the screen? Should another component move relative to a component, or be fixed?
精彩评论