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

【背景】

需要把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()

了。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量