它是UINavigationControllerDelegate
的委托(delegate)方法。 Swift 和 Objective-C 的返回值是不同的。见:
objective-C :
- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
swift
func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> Int
当我使用 objective-c 时,它工作正常。但是,当我使用 Swift 时,我无法在方法中返回正确的值。见下图:
谁能解释一下为什么,有什么解决办法吗?
最佳答案
尝试使用这个
func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController!) -> Int
{
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
更多详情请点击此处
强制转换的原因在 Objective-C 中
上面的函数返回类型是 NSInteger,因为 UIInterfaceOrientationPortrait 是一个 NSInteger 类型的枚举。
而Swift则是以struct的形式定义。
struct UIInterfaceOrientationMask : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)
static var Portrait: UIInterfaceOrientationMask { get }
static var LandscapeLeft: UIInterfaceOrientationMask { get }
static var LandscapeRight: UIInterfaceOrientationMask { get }
static var PortraitUpsideDown: UIInterfaceOrientationMask { get }
static var Landscape: UIInterfaceOrientationMask { get }
static var All: UIInterfaceOrientationMask { get }
static var AllButUpsideDown: UIInterfaceOrientationMask { get }
}
关于ios - Swift 中 navigationControllerSupportedInterfaceOrientations 的返回值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912170/