gpt4 book ai didi

ios - 不能自动改变方向

转载 作者:行者123 更新时间:2023-11-29 13:00:41 26 4
gpt4 key购买 nike

我有一个 TabBarController,里面有两个 UIViewControllers,为了能适应景观,我在第一个 ViewController 中添加了以下代码:

 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ return YES; }

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//handle with portait
}else{
//handle with landscape
}
}

实际上它有效,但我遇到了一个问题:当我在第二个 ViewController 中改变方向并返回第一个 ViewController 时,它不能自动改变方向,我需要手动改变方向,所以我想要一个解决方案来避免这个

最佳答案

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationLandscapeLeft];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:true];

这样就可以了。并且不要忘记在此方法调用后更改 View 的框架,例如:

- (void)viewDidLoad
{
[super viewDidLoad];

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationLandscapeLeft];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:true];

// Screen is turned now, change all views their frames
[myButton setFrame:CGRectMake(480, 20, 0, 0)];
[myLabel setFrame:CGRectMake(480, 50, 0, 30)];
}

关于ios - 不能自动改变方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19860627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com