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/