如何在SWIFT中将单视图控制器的方向锁定为纵向模式因为我的应用程序得到了所有方向的支持。我只想锁定特定UIViewController的纵向模式。(例如,假设它是选项卡式应用程序,并且当signin View以模态方式出现时,我只希望该signin View只有在用户如何旋转设备或当前设备方向的情况下才能进入纵向模式)
3 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
SWIFT 3&4
/// set orientations you want to be allowed in this property by defaultvar orientationLock = UIInterfaceOrientationMask.allfunc a
pplication(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock}struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation }
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}} override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppUtility.lockOrientation(.portrait)
// Or to rotate and lock // AppUtility.lockOrientation(.portrait, andRotateTo: .
portrait)}override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Don't forget to reset when view is being removed AppUtility.lockOrientation(.all)}如果iPad或通用应用程序
supportedInterfaceOrientationsFor
阿晨1998
TA贡献2037条经验 获得超6个赞
SWIFT 4
var orientationLock = UIInterfaceOrientationMask.allfunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window:
UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock}struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation }
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}}您的视图控制器
override func viewWillAppear(_ animated: Bool) {AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo:
UIInterfaceOrientation.portrait)
}override func viewWillDisappear(_ animated: Bool) {
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)
}
哔哔one
TA贡献1854条经验 获得超8个赞
override func viewDidLoad() {
super.viewDidLoad()
// Force the device in portrait mode when the view controller gets loaded UIDevice.currentDevice().
setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") }override func shouldAutorotate() -> Bool {
// Lock autorotate return false}override func supportedInterfaceOrientations() -> Int {
// Only allow Portrait return Int(UIInterfaceOrientationMask.Portrait.rawValue)}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
// Only allow Portrait return UIInterfaceOrientation.Portrait}func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All}- 3 回答
- 0 关注
- 782 浏览
添加回答
举报
0/150
提交
取消
