问题
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.
- Add top, bottom, leading, trailing, width and height constraints to the box.
- Set the priority of the top, bottom, leading and trailing constraints to High (750).
- Set the relation for the width and height constraints to Less Than or Equal (≤). The default value is Equal.
- Set the priority of the width and height constraints to Required (1000).
- 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