ios - 更改方向后更改大小

标签 ios swift size orientation

func SetDeviceFontSizes()
{
    ...
    imgView.frame.size.width = 375        
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    SetDeviceFontSizes()
}

override func viewWillLayoutSubviews() {
    SetDeviceFontSizes()
}

override func viewDidLoad() {
    super.viewDidLoad()
    SetDeviceFontSizes()
}            

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)        
    SetDeviceFontSizes()
}

我想设置标签和图像的尺寸(对于旧设备,这些尺寸会很小)。 但是,当我更改设备的方向时,尽管有函数 SetDeviceFontSizes(),但大小仍会返回默认值。 更改方向后如何设置框架大小?

最佳答案

var m_CurrentOrientation = UIDeviceOrientation()

在您的 viewdidload() 中添加这一行

 UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.deviceOrientationDidChange), name: .DidChangeNotification, object: nil)

在if条件下执行你的任务

func OrientationDidChange(notification: NSNotification) {
    var Orientation = UIDevice.currentDevice().orientation
    if Orientation == .LandscapeLeft || Orientation == .LandscapeRight {

    }
    else if Orientation == .Portrait {

    }

}

关于ios - 更改方向后更改大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40239607/

相关文章:

ios - 带有动态表格单元格的侧面菜单将导航页面,但导航 Controller 为 nil

ios - UISplitViewController - 防止在 iPhone 6 plus 上横向拆分

ios - 无法分配给值 : 'i' is a 'let' constant in swift

ios - 如何使用 CallKit 通过 CXCallUpdate 添加 customInfo?

swift - 我如何从iOS系统词典中获取单词含义

ios - 如何在 Objective C 中将视频压缩到精确的大小?

ios - 将文件 url 从 documentDirectory 加载到 TableView

xcode - 如何获取变量保存在 SpriteKit swift Xcode 的所有场景中

java - JPanel 不需要的位置和/或大小

java - 如何强制设置 Java (.jar) 应用程序窗口的大小?