You can go the autolayout "way" as vacawama mentioned (How to get exactly the same point on different screen sizes?
) or implement and override
-(void)layoutSubviews { ... } //with Objective-C
override func layoutSubviews() { ... } //with swift
of your UIView
. Or if you used a UIViewControler
the following
-(void)viewDidLayoutSubviews { ... } //in Objective-C
override func viewDidLayoutSubviews() { ... } //with swift
Those methods are called when the UI needs to layout its contents, so also if your View is scaled.
It is also called when your UIView is allocated with -(instancetype)initWithFrame:(CGRect)rect
in Objective-C or super.init(frame:CGRect)
in swift. By the way instancing with Storyboard is calling those methods also, which is one of the reasons why you declare in Interface Builder what class a dropped UIView or ViewController is.
To know more about layoutSubviews have a look at this answer https://stackoverflow.com/a/5330162/1443038..
class MyView: UIView {
// assuming you have declared init() and so on
// and a var myButton : UIButton
override func layoutSubviews() {
myButton.frame = CGRect(x: 0, y:0, width:self.frame.size.width * 0.2, height:self.frame.size.height * 0.2)
}
}
class MyViewController : UIViewController {
// assuming you have declared init() or init(frame:) and so on
// and a var myButton : UIButton
override func viewDidLayoutSubviews() {
//same as example above, but here you ask self.view.frame
myButton.frame = CGRect(x: 0, y:0, width:self.view.frame.size.width * 0.2, height:self.view.frame.size.height * 0.2)
}
}