2016-10-25 14 views
1
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()にもかかわらず、サイズはデフォルト値に戻ります。 方向変更後にフレームサイズを設定するにはどうすればよいですか?変更後のサイズの変更

+1

http://stackoverflow.com/a/25667424/4475605 – Adrian

+0

ありがとうございます!それは動作します – Kirill

+0

Stylistically、あなたはそれを少しきれいにすることができます。私はライフサイクルメソッドをセクション(viewDidLoad、viewWillAppear、viewDidAppear)に置き、 'setDeviceFontSizes'メソッドをその下に置きます。また、メソッドは大文字ではなく 'camelCase'にする必要があります。 – Adrian

答えて

0
var m_CurrentOrientation = UIDeviceOrientation() 

(あなたのviewDidLoadで、この行を追加します)

UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() 

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

条件

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

    } 
    else if Orientation == .Portrait { 

    } 

} 
+0

はSwift 3で動作しませんでした – Kirill

0

場合、私は、これはあなたの問題を解決するかわからない中であなたのタスクを実行しますしかし、あなたはviewWillLayoutSubviewsviewWillTransition

にスーパーを呼び出す必要があります
関連する問題