Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a cleaner way to edit 1 of the 4 CGRect values of a UIView's frame?

generally when I want to move a label to the right 20px, or increase just the width of a view, I go through one of the following avenues

label.frame = CGRectMake(label.frame.origin.x+20, label.frame.origin.y, label.frame.size.width, label.frame.size.height);

or

CGRect viewFrame = view.frame;
viewFrame.x += 20;
view.frame = viewFrame;

I don't particularly like the amount of code that goes into either variation and was hoping you guys knew a shortcut that I hadn't discovered

like image 778
korben Avatar asked Dec 28 '13 15:12

korben


2 Answers

A better method of moving to the right is the CGRectOffset macro:

label.frame = CGRectOffset(label.frame, 20.0f, 0.0f);

I find that this a clearer expression of the intent of my code.

like image 112
ColinE Avatar answered Nov 13 '22 12:11

ColinE


Just to add:

Here's an article with nice CGRect tricks (Shrinking, Expanding, Edge Insetting, Intersecting)

like image 44
Stas Avatar answered Nov 13 '22 12:11

Stas