How to set dynamic width & height of a view in autolayout in iOS?

后端 未结 4 436
庸人自扰
庸人自扰 2021-01-04 11:12

I have a UIView in my super view.I have set it some constraints.Now I want the size of view should be different on different devices.If I set the fix width & height then

相关标签:
4条回答
  • 2021-01-04 11:34

    Use Size Classes. It helps to have different settings for different devices. http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial https://youtu.be/IwSTXY0awng?t=2m12s

    0 讨论(0)
  • 2021-01-04 11:42

    Try to follow the screen shots as below:

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    You will get the result as below

    enter image description here

    0 讨论(0)
  • I have written a blog article about autolayouting for dynamically changing layouts.
    For each screen size you can programmatically add the constraints.
    Algorithm

    1. Get the current device size
    2. Get the required constraints according to the device size
    3. Then programmatically add the constraints


    Changing according to orientation

    • Listen to the UIDeviceOrientationDidChangeNotification notifiation
    • Add new constraints according to the notification
    • Then call setNeedsUpdateConstraints
      For more info and a example code, look at the blog article and this github repo
    0 讨论(0)
  • 2021-01-04 11:45

    enter image description hereThe most simple way is create a Outlet of your width and height constraint.

    Then dynamically change it in viewDidLoad according to phone.

    To check which phone it is u can check the device height like 480 for iPhone 4S ,568for iPhone 5,5S and etc

    @interface ScoreListViewController (){
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
    
    @end
    
    
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Set the value according to your requirement
    if(4S){
    self.heightConstraint.constant=100;
    }
    
     }
    
    0 讨论(0)
提交回复
热议问题