How to handle UI components when device orientation for iPhone/iPad changes?
-
03-06-2021 - |
質問
I have several components on my UIView like textfields, labels, tables, buttons. I want to handle device orientation changes. How to make these components arrange at right place automatically? Or do I need to re-frame them?
Please help.
Thanks
解決
You may use Autosizing option for the orientation changes.No need to write even a single line of code.
他のヒント
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
// set you subviews,Label button frame here for landscape mode,,
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
// set you subviews,Label button frame here for portrait-mode,
}
}
//Don't forget to add this Delegate
Method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
return YES;
}
Hope, this will help you..
Try this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
// portrait Arrangement
}
else
{
// Landscape Arrangement
}
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
所属していません StackOverflow