iOS:如何让`uiview`支持纵向和横向?
iOS:How to let a `uiview` support portrait and landscape orientation?
我有一个 photoBroser
是继承的 uiview
:
@interface SDPhotoBrowser : UIView <UIScrollViewDelegate>
但是现在我有一个要求让photoBroser
支持iphone
纵向和横向,以及如何做到这一点?
编辑:在我的应用程序中,我的应用程序仅支持 portrait
方向,仅在我的 photoBrowser
即 uiview
中,我想支持 portrait and landscape
方向。
编辑:我应用了J.Hunter
的答案,他的答案非常好,在实际编码中,我建议一个函数来改变portrait and landscape orientation
。当我解雇我的photoBrowser
,出现问题,在我的 photoBrowser
关闭后,设备还 landscape orientation
,所以我使用下面的代码让我的设备 portrait orientation
,然后我的 photoBrowser
] 解雇,所以有合理的表现。
//(force landscape:[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];)
//(force Portrait:[self interfaceOrientation:UIInterfaceOrientationPortrait];)
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// from 2
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
此功能可能有助于完善 support portrait and landscape orientation
代码。
现在我知道你的应用程序只支持纵向。在特殊视图控制器中支持纵向和横向将需要您的两个步骤。
我想你使用 Objective-C
1. 在 AppDelegate.h
中添加一个名为 @property (nonatomic, assign) BOOL shouldRotation;
的 属性
2、在'AppDelegate.m'中实现UIApplicationDelegate
-- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
的定位方法,就像
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.shouldRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
else {
return UIInterfaceOrientationMaskPortrait;
}
}
现在您可以在任何您想要的 UIViewController 中支持横向。
- (void)viewDidLoad {
[super viewDidLoad];
// enable landscape orientation
[UIApplication sharedApplication].delegate.shouldRoration = YES;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// disable landscape orientation
[UIApplication sharedApplication].delegate.shouldRoration = NO;
}
希望对您有所帮助。
PS,还有一种方法可以解决:
- 您的应用支持所有方向
- 你所有的 UIViewController 覆盖
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
、return UIInterfaceOrientationMaskPortrait
或 UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape
我有一个 photoBroser
是继承的 uiview
:
@interface SDPhotoBrowser : UIView <UIScrollViewDelegate>
但是现在我有一个要求让photoBroser
支持iphone
纵向和横向,以及如何做到这一点?
编辑:在我的应用程序中,我的应用程序仅支持 portrait
方向,仅在我的 photoBrowser
即 uiview
中,我想支持 portrait and landscape
方向。
编辑:我应用了J.Hunter
的答案,他的答案非常好,在实际编码中,我建议一个函数来改变portrait and landscape orientation
。当我解雇我的photoBrowser
,出现问题,在我的 photoBrowser
关闭后,设备还 landscape orientation
,所以我使用下面的代码让我的设备 portrait orientation
,然后我的 photoBrowser
] 解雇,所以有合理的表现。
//(force landscape:[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];)
//(force Portrait:[self interfaceOrientation:UIInterfaceOrientationPortrait];)
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// from 2
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
此功能可能有助于完善 support portrait and landscape orientation
代码。
现在我知道你的应用程序只支持纵向。在特殊视图控制器中支持纵向和横向将需要您的两个步骤。
我想你使用 Objective-C
1. 在 AppDelegate.h
中添加一个名为 @property (nonatomic, assign) BOOL shouldRotation;
的 属性
2、在'AppDelegate.m'中实现UIApplicationDelegate
-- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
的定位方法,就像
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.shouldRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
else {
return UIInterfaceOrientationMaskPortrait;
}
}
现在您可以在任何您想要的 UIViewController 中支持横向。
- (void)viewDidLoad {
[super viewDidLoad];
// enable landscape orientation
[UIApplication sharedApplication].delegate.shouldRoration = YES;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// disable landscape orientation
[UIApplication sharedApplication].delegate.shouldRoration = NO;
}
希望对您有所帮助。
PS,还有一种方法可以解决:
- 您的应用支持所有方向
- 你所有的 UIViewController 覆盖
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
、returnUIInterfaceOrientationMaskPortrait
或UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape