最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift中代码出错:Cannot override shouldAutorotateToInterfaceOrientation which has been marked unavailable

Swift crifan 3205浏览 0评论

【背景】

需要把ObjC代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Only rotate if all child view controllers agree on the new orientation.
for (UIViewController *viewController in self.viewControllers)
{
if (![viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation])
return NO;
}
return YES;
}

改为iOS 9中的swift代码。

结果遇到了:

shouldAutorotateToInterfaceOrientation

好像不支持了:

swift:53:19: Cannot override ‘shouldAutorotateToInterfaceOrientation’ which has been marked unavailable

需要搞懂最新的写法。

【解决过程】

1.去搜:

swift shouldAutorotateToInterfaceOrientation

Cannot override shouldAutorotateToInterfaceOrientation which has been marked unavailable

参考:

iOS6不支持shouldAutorotateToInterfaceOrientation – starryheavens的专栏 – 博客频道 – CSDN.NET

ios6 – shouldAutorotateToInterfaceOrientation not being called in iOS 6 – Stack Overflow

ios – Why shouldAutorotateToInterfaceOrientation method marked as unavailable? – Stack Overflow

去改为:

shouldAutorotate

2.写成:

    override func shouldAutorotate(toInterfaceOrientation: UIInterfaceOrientation) -> Bool {
       
    }

结果也出错:

swift:53:19: Method does not override any method from its superclass

发现是:

最新的代码改为:shouldAutorotate()

所以上述函数由于参数不对,也不是overvide

shouldAutorotate()

所以:

 

3.最后写成

    override func shouldAutorotate() -> Bool {
        // Only rotate if all child view controllers agree on the new orientation.
        for viewController in self.viewControllers {
            if (viewController.shouldAutorotate() != true) {
                return false
            }
        }
       
        return true
    }

即可。

【总结】

shouldAutorotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation)

已经废弃不用了。

改用:

shouldAutorotate()

了。

转载请注明:在路上 » 【已解决】swift中代码出错:Cannot override shouldAutorotateToInterfaceOrientation which has been marked unavailable

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.192 seconds, using 22.05MB memory