Sunday, May 1, 2011

Why can't I set the bounds rectangle width of an UIImageVies?

When I try this

myUIImageView.bounds.size.width = 50.0f;

then I get an error: invalid lvalue in assignment

But when I do this, it compiles. Although it's a lot of extra-work

CGRect newRect = myUIImageView.bounds;
newRect.size.width = 50.0f;
myUIImageView.bounds = newRect;

Although that compiles, nothing will happen. My UIImageView's frame is 300 wide and filled with an image that is exactly 300px width. It fits perfectly. So now I wanted to stretch that image (just for fun), so I would have to make the bounds rectangle smaller in width. Lets say 50.0. But unfortunately, the image does not get stratched like expected. Just nothing happens. Two lines below this code, I move the image around. That works.

From stackoverflow
  • properties size and orign are readonly.

    bounds is not readonly.

    Let is why you can change bounds, but can't change size

    Thanks : Thanks. What does the "Ivalue" in the error message "invalid lvalue in assignment" mean?
    oxigen : lvalue is 'left-value' expression that can stay at the left of '='
  • This works (for me);

    CGRect frame = myUIImageView.frame;
    frame.size.width = 50.0;
    myUIImageView.frame = frame;
    

    Try that..

    Thanks : yeah, that works. But I don't want to change the frame. I want to change the bounds.
    MiRAGe : Then I guess I'm clueless on what the difference is.
    Thanks : there's a big difference. See http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/Coordinates.html
  • -[UIView bounds] returns a new CGRect containing the x, y, width and height values of the view's bounds. Changing the values on this new CGRect would have no effect and GCC realizes this.

    In order to change the width you have to get the bounds from the view, store them somewhere, change the width value on the stored CGRect and inform the view of its new bounds using -[UIView setBounds:]

    Thanks : Thanks! I tried that, but this also has no effect to my UIImageView. Is there some special content mode I have to set up in order to stretch or jar my image?
    rpetrich : UIImageView doesn't manipulate the image at all, it just draws it as-is inside the bounds. To scale the view, simply call [view setTransform:CGAffineTransformMakeScale(0.5f, 1.0f)];

0 comments:

Post a Comment