2

Im aware I can change the position of my image view like this

myImageView.frame = CGRectMake(99, 34, 32, 32);

But how do I change just the x value leave the rest as they are? I realize this is a simple question but because I don't know how to word the search Im having difficulty finding a solution. Thanks

6 Answers 6

8
//capture frame
CGRect thisRect = myImageView.frame;

//modify required frame parameter (.origin.x/y, .size.width/height)
thisRect.origin.x = 0;

//set modified frame to object
[myImageView setFrame:thisRect];
6
  • @Rookie although this solution works I really believe that, in this case, CGRectOffSet is cleaner. Check my answer for more details. Jan 31, 2014 at 18:54
  • @TiagoAlmeida : true neater but the link you provided made it seem like alot of reading. this link on CGRectOffset is alot more simpler :) Jan 31, 2014 at 19:01
  • @staticVoidMan agree. Was just to give a good amount of solutions for this :p Jan 31, 2014 at 19:12
  • This works great as I want to change the position according to the current value. so I'm doing something like this thisRect.origin.x += 25; Don't see how I can do that using CGRectOffset
    – 4GetFullOf
    Jan 31, 2014 at 19:15
  • @Rookie : for that it should be CGRectOffset(myImageView.frame, 25, 0) for incrementing the value of x by 25 Jan 31, 2014 at 19:19
5

You have some solutions to this issue but usually, if you want to simplify, you end up using a category like UIView+Position (UIView+Position.h UIView+Position.m) or UIView Helpers. Although this might be an overkill.

Another alternative is to use CGRect auxiliary methods like this:

myImageView.frame = CGRectOffset(myImageView.frame, 10, 0);

For more information on solutions for this problem read this post.

2
  • For me this is by far the cleanest and Objective-C friendly option. May 26, 2014 at 11:07
  • 1
    this should've been an accepted answer. Much a cleaner way to do the task
    – Alehar
    Dec 18, 2016 at 21:57
1

@staticVoidMan basically already said it, but if you want to keep the current scope clean and not pollute it with temporary variables, you can take advantage of a GCC extension and do this:

[self.view setFrame:({

    CGRect frame = [self.view frame];
    frame.origin.x = 0;
    frame;

})];

Note that while I said it's a GCC extension, it works fine with Clang (which, for the most parts, is compatible with GCC when it comes to these things).

0

Create a new rectangle and modify the parts you want, then assign the frame to that one.

CGRect newFrame = myImageView.frame;
newFrame.origin.x = 49;

myImageView.frame = newFrame;

You can access the width similarly:

newFrame.size.width = 55;

etc.

0

A Swift alternative using an extension can be found here. Example from above:

myImageView.x = 99

or

myImageView.left = 99
0

In Swift you can now do:

view.frame = view.frame.offsetBy(dx: 0, dy: -100)

to shift up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.