问题
I'm working on a project in which when the user press a button a video will be played, I have to program this app for all orientations. I have added images as background for buttons given those buttons position based on the size of the superview like this:
button1.frame = CGRectMake(0.15 * (self.view.frame.size.width),0.15 * (self.view.frame.size.height), 0.2 * (self.view.frame.size.width), 0.2 * (self.view.frame.size.height));
When I start the app in portrait it's view is fine and also when I rotate it works fine :
But if I start my app in landscape it shows a view like this:
I'v used a lot of code to resize views and subviews:
-(void) viewDidLoad
{
...
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.view setAutoresizesSubviews:YES];
button1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
...
}
and in didRotateFromInterfaceOrientation() and willRotateToInterfaceOrientation():
...
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.view setAutoresizesSubviews:YES];
button1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
...
I'v also specified YES for return in shouldAutoRotate(). What am I lacking here? Any help will be appreciated.
回答1:
you have to declaration below and all give them in viewDidLoad but frame give in
this code
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
}
来源:https://stackoverflow.com/questions/20000503/issue-with-device-orientation-and-uiview-during-startup