How can I set maximum width of a view in IOS?

蹲街弑〆低调 提交于 2019-12-23 06:49:19

问题


My app have a basic login screen, an outer box and in it, some textfield and buttons. I set the box to fill the screen. However, on some devices this box will to big. I want to set a maximum width and height to it.

How do I set maximum width and height to a view?


回答1:


You can use auto-layout constraints so that the box adapts to the screen size, but does not exceed a given width and height. To do this, set a "less than or equal to" constraint on the width and height.

  1. Add top, bottom, leading, trailing, width and height constraints to the box.
  2. Set the priority of the top, bottom, leading and trailing constraints to High (750).
  3. Set the relation for the width and height constraints to Less Than or Equal (≤). The default value is Equal.
  4. Set the priority of the width and height constraints to Required (1000).
  5. Set the constant of the width and height constraints to the maximum size you want the box to be.




回答2:


If you want to give a View's frame to fix all devices then use this width and height

self.view.frame.size.width,self.view.frame.size.height

Or You can give auto layouts Top Bottom Trailing and Leading constraints by dragging the View to its edges.




回答3:


Simple you use only four constraints (i.e Top space,Bottom space,Leading space,Trailing space) for that container view.




回答4:


If you are using Interface Builder you could :

  • ctrl + clic your view and drag the blue arrow to the superview.

  • Hold shift and select "equal width" and "equal height"

  • Then go to the constraints of your view (little ruler icon) and set a multiplier like 0.5 to make your view width/height equal to half of its superview respective dimension.




回答5:


UIView *viw=[UIView alloc]init];
viw.frame=CGRectMake(x,y,CGFLOAT_MAX,h);



回答6:


Try following code it may helps you

yourviewObject.frame=CGRectMake(yourviewObject.frame.origin.x,yourviewObject.frame.origin.y,self.view.frame.size.width,yourviewObject.frame.size.height)


来源:https://stackoverflow.com/questions/38308376/how-can-i-set-maximum-width-of-a-view-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!