【问题标题】:Programmatically resizing views to UIInterfaceOrientationMaskLandscapeLeft以编程方式将视图大小调整为 UIInterfaceOrientationMaskLandscapeLeft
【发布时间】:2016-09-07 04:47:58
【问题描述】:

我知道这个问题可能已经回答了好几次了,但是,我确实尝试了在 StackOverflow 上可以找到的所有可能的东西,但没有成功。

基本上,我有这样初始化的自定义 UIView 类:

MyUIView *test = [[MyUIView alloc] initWithFrame:self.view.bounds];

然后,我有一个 NSTimer 计划在 5 秒内通过这个调用关闭:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    if (!self.isRotated) {
        self.isRotated = YES;
        appDelegate.allowLandscape = YES;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
    }else{
        self.isRotated = NO;
        appDelegate.allowLandscape = NO;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
    }

基本上是这样的:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.allowLandscape) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

如果我只是在viewDidLoad 方法中将self.allowLandscape 设置为YES,它就可以正常工作。但是,如果我在延迟 5 秒后调用 NSTimer 方法,则 UIView 不会被转换为 LandscapeLeft

我可以通过使用约束来解决这个问题,但是,在这个项目中,我只使用程序化大小。

【问题讨论】:

    标签: ios objective-c mobile view resize


    【解决方案1】:

    我建议像这样使用 AutoresizingMasks

    view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    

    这将根据超类自动调整视图大小。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      相关资源
      最近更新 更多