Question

Often I need to simply move the view 5 pixels down. My approach is like this

view.frame = CGRectMake(view.frame.origin.x,
                        view.frame.origin.y + 5,
                        view.frame.size.width,
                        view.frame.size.height);

Isn't there some easier way? :-/

Was it helpful?

Solution 5

Perhaps I should write a bunch of categories for the UIView class.

Something like

-(void)changeFrameOriginHorizontally:(NSInteger)paramHorizontalChange
                          vertically:(NSInteger)paramVerticalChange;

-(void)changeFrameSizeHorizontally:(NSInteger)paramHorizontalChange
                        vertically:(NSinteger)paramVerticalChange;

The main reason I do not like the solution in my original post is that it is very code sense unfriendly and too verbose.

I would allow for negative and positive values and nil for no change.

What do you guys think?

OTHER TIPS

another solution

view.frame = CGRectOffset( view.frame, 0, 5 );

I've got a macro on github that will let you do it like

@morph(view.frame, _.origin.y += 5);

Or if you wanted to change y and height

@morph(view.frame, {
    _.origin.y += 5;
    _.size.height -= 5;
});

slightly better:

view.center = CGPointMake(view.center.x, view.center.y+5);

I have macro's for these:

#define VIEW_INCREASE_Y(view, value) view.frame = CGRectMake(view.frame.origin.x,view.frame.origin.y+value,view.frame.size.width,view.frame.size.height)

Then you can do:

VIEW_INCREASE_Y(customView, 25);

If you want to go the category route you should give it a prefix to avoid method name collisions.

e.g.

- (void)ps_addOffset:(CGPoint)offset;
{
    self.frame = CGRectIntegral(CGRectOffset(self.frame, offset.x, offset.y));
}

I like to use Macros:

#define width(a) a.frame.size.width
#define height(a) a.frame.size.height
#define top(a) a.frame.origin.y
#define left(a) a.frame.origin.x
#define bottom(a) top(a)+height(a)
#define right(a) left(a)+width(a)

#define FrameReposition(a,top,left) a.frame = CGRectMake(x, y, width(a), height(a))

.....

FrameReposition(myView,0,0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top