Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a views frame width? (Why can't I set it directly?)

I want to reset a UIView's frame size so I wrote:

view.frame.size.width = x;

but it doesn't work, can anyone tell me why?

like image 884
Kaibin Avatar asked May 22 '12 08:05

Kaibin


People also ask

How do I change the frame size in Swift?

1) Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select "Equal heights". 2)Then go to size inspector of the frame, click edit "Equal height to Superview" constraint, set the multiplier to 0.7 and hit return.

What is a frame in SwiftUI?

SwiftUI's built-in frame modifier can both be used to assign a static width or height to a given view, or to apply “constraints-like” bounds within which the view can grow or shrink depending on its contents and surroundings.


2 Answers

you can't set width directly, do this

CGRect frm = view.frame;
frm.size.width = x;
view.frame = frm;
like image 171
Saad Avatar answered Oct 20 '22 11:10

Saad


When you call view.frame you get a copy of the frame rect property so setting it is with frame.size.width changes the width of the copy and not the view's frame size

like image 31
giorashc Avatar answered Oct 20 '22 11:10

giorashc