Reputation: 2207
I've just started working with constraints, and I'm finding it onerous to have to set both the location and the size of a UIView using constraints--four constraints seems excessive. (I don't doubt that it's possible to do with fewer constraints; I'm just still working with a very basic understanding!) Is it possible to set the size of a UIView, therefore, not using constraints, but then set the location using constraints? The only non-constraint ways I know to set size also involve setting origin.
Upvotes: 3
Views: 10094
Reputation: 12081
If you are using AutoLayout to manage view position and size it's not advised to adjust a view's frame. Most of the time this won't work anyway.
But even AutoLayout needs to know about size and position. So you'll always end up having to specify both. If like you ask it would be possible to set less than 4 constraints (by not specifying it's position for example), AutoLayout would not know where to show the view.
What is it you find hard about having to specify 4 constraints? This is done automatically for you in your XIB/Storyboard file. If you are writing everything in code checkout the convenience ASCII-art-format strings which lets you specify position and sizing using a 'visual' string representation.
Upvotes: 4
Reputation: 1507
Supposedly:
The geometry of a view is defined by its frame, bounds, and center properties. The frame defines the origin and dimensions of the view in the coordinate system of its superview and is commonly used during layout to adjust the size or position of the view. The center property can be used to adjust the position of the view without changing its size. The bounds defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing code. The size portion of the frame and bounds rectangles are coupled together so that changing the size of either rectangle updates the size of both.
So, theoretically, you can change the size of the bounds, where the origin is always 0,0.
Upvotes: 2
Reputation: 119242
Setting the bounds
property of a view will change its size without you explicitly setting an origin - from the docs:
Changing the bounds size grows or shrinks the view relative to its center point.
You can try setting an appropriate resizing mask (probably flexible margins all round?) which will be translated into constraints for you, and then use positioning constraints to move it.
However, I'd personally recommend going all in with constraints rather than mixing and matching. The visual format would allow you to specify size and position pretty simply.
Upvotes: 1
Reputation: 957
It seems like you're just trying to set the frame of the view (both the origin, and size).
You can use something like this to do so:
CGRect newFrame = CGRectMake( xCoordinate, yCoordinate, width, height);
myView.frame = newFrame;
Where xCoordinate, yCoordinate, width and height are whatever you'd like.
Hope that helps! Let me know if it's not the answer you were looking for and I'll try to help.
Upvotes: 2