在创建时停止 UIViewController 的自动旋转
Stop autorotation of UIViewController at creation time
有什么方法可以设置 UIViewController 对象启动时的自动旋转行为吗?
我希望它永远不会旋转,所以我尝试了这个:
UIViewController *myViewController = [[UIViewController alloc]init];
myViewController.shouldAutorotate = NO;
看似合乎逻辑,但行不通。获取错误信息:
没有 setter 方法 'setShouldAutorotate:' 分配给 属性
还是只能通过子类化才能实现?
你应该这样:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Return Value
A bit mask specifying which orientations are supported.
See UIInterfaceOrientationMask for valid bit-mask values. The value
returned by this method must not be 0.
Discussion
When the user changes the device orientation, the system
calls this method on the root view controller or the topmost presented
view controller that fills the window. If the view controller supports
the new orientation, the window and view controller are rotated to the
new orientation. This method is only called if the view controller's
shouldAutorotate method returns YES.
Override this method to report all of the orientations that the view
controller supports. The default values for a view controller's
supported interface orientations is set to
UIInterfaceOrientationMaskAll for the iPad idiom and
UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.
The system intersects the view controller's supported orientations
with the app's supported orientations (as determined by the Info.plist
file or the app delegate's
application:supportedInterfaceOrientationsForWindow: method) to
determine whether to rotate.
有什么方法可以设置 UIViewController 对象启动时的自动旋转行为吗? 我希望它永远不会旋转,所以我尝试了这个:
UIViewController *myViewController = [[UIViewController alloc]init];
myViewController.shouldAutorotate = NO;
看似合乎逻辑,但行不通。获取错误信息:
没有 setter 方法 'setShouldAutorotate:' 分配给 属性
还是只能通过子类化才能实现?
你应该这样:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Return Value
A bit mask specifying which orientations are supported. See UIInterfaceOrientationMask for valid bit-mask values. The value returned by this method must not be 0.
Discussion
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns YES.
Override this method to report all of the orientations that the view controller supports. The default values for a view controller's supported interface orientations is set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.
The system intersects the view controller's supported orientations with the app's supported orientations (as determined by the Info.plist file or the app delegate's application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.