xcode 如何根据设备方向 iOS 6 自动旋转 UIView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14120375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to autorotate UIView based on device orientation iOS 6?
提问by Newbee
I have started my project with iOS 5.0, Now updated to iOS 6, I am facing problem in doing orientation, To test I have created a sample application, and added the following code in delegate...
我已经用 iOS 5.0 开始了我的项目,现在更新到 iOS 6,我在做方向时遇到了问题,为了测试我已经创建了一个示例应用程序,并在委托中添加了以下代码...
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
And in my view controller I have implemented the following methods...
在我的视图控制器中,我实现了以下方法......
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate
{
return YES;
}
All the above I have done after googled about iOS 6.0,when I load application, based on the option given in supportedInterfaceOrientations view is loading that is fine, now when I changes my device orientations... view is not changing accordingly at run time, how do I change my view based on the device orientation?
以上所有我在谷歌搜索 iOS 6.0 后完成的,当我加载应用程序时,基于 supportedInterfaceOrientations 视图中给出的选项加载很好,现在当我改变我的设备方向时......视图在运行时没有相应改变,如何根据设备方向更改我的视图?
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[notificationCenter addObserver:self
selector:@selector(deviceOrientationDidChange)
name:UIDeviceOrientationDidChangeNotification object:nil];
Do I need to add observer like above and changes it programmatically? or Will the view change automatically by detecting device orientation?
我是否需要像上面那样添加观察者并以编程方式更改它?或者视图会通过检测设备方向自动改变吗?
回答by jcesarmobile
try this
尝试这个
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (BOOL) shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscape;
}
The previous code should work on iOS6
之前的代码应该适用于 iOS6
Anyways, I use this code on one of my projects. I have a boolean _firstTime
, and I set it to YES
before showing the viewController
, then I change it to NO
after the viewController
appears
无论如何,我在我的一个项目中使用此代码。我有一个布尔值_firstTime
,我YES
在显示之前将其设置为viewController
,然后NO
在viewController
出现之后将其更改为
- (NSUInteger)supportedInterfaceOrientations {
if (_firstTime) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskAll;
}
}
- (BOOL) shouldAutorotate {
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (_firstTime) {
return return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
return YES;
}
}
回答by Paresh Navadiya
Change in these method:
这些方法的变化:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
Note: No need to add observer
for device notification
.Given below method will specify orientation status
注意:无需添加observer
for device notification
.Given 下面的方法将指定方向状态
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
EDIT: Provide all your views
will automasking
according to requirement
编辑:根据您的views
意愿提供您的所有意愿automasking
requirement
EDIT: If using UINavigation Controller
then its child View Controller's
supportedInterfaceOrientations
and shouldAutorotate
method will not be called
.
编辑:如果使用UINavigation Controller
那么它的child View Controller's
supportedInterfaceOrientations
和shouldAutorotate
方法将不是called
。
Refer thislink.
参考这个链接。
回答by Newbee
Problem is with my iPad. it has a button to restrict auto rotation change... I was not aware initially, as I was worked fully with iPod.. Thanks all for your helps.
问题出在我的 iPad 上。它有一个按钮来限制自动旋转变化......我最初并不知道,因为我完全使用 iPod ......感谢大家的帮助。