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()
}

我想设置标签和图像的大小(对于旧设备,这些大小将很小)。
但是,当我更改设备的方向时,大小将返回到默认值,而不管函数setDeviceFuntsizes()。
如何在更改方向后设置帧大小?

最佳答案

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/

10-13 07:46
查看更多