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

I want to set sizes for labels and images (for old devices these sizes will be small). But when I change orientation of device, the sizes return to default values in spite of function SetDeviceFontSizes(). How can I set frame sizes after change orientation?

Kirill
  • 1,412
  • 4
  • 21
  • 45
  • 1
    http://stackoverflow.com/a/25667424/4475605 – Adrian Oct 25 '16 at 12:21
  • Stylistically, you could clean it up a bit. I'd put lifecycle methods in a section (viewDidLoad, viewWillAppear, viewDidAppear) and your `setDeviceFontSizes` method below that. Also, methods should be in `camelCase`, not capitalized. – Adrian Oct 25 '16 at 12:59

3 Answers3

0
var m_CurrentOrientation = UIDeviceOrientation()

add this line in your viewdidload()

 UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()

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

perform your task in if condition

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

    }
    else if Orientation == .Portrait {

    }

}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

i'm not sure this will solve your problem, but you should call super in viewWillLayoutSubviews and viewWillTransition

0

You can use UITraitCollection. Refer to this link How to translate this objective c traitCollection to swift?

Do not call anything in viewWillLayoutSubviews() function.

Hope it helps you.

Community
  • 1
  • 1
Sandeep
  • 110
  • 9