Autolayout UIImageView with programatic re-size not following constraints

筅森魡賤 提交于 2019-12-09 07:03:43

问题


Sorry for the bother but I'm trying to lazy load several imageViews and then resize it proportionately to the content in a UITableView. I'm also trying (Unwisely perhaps?) to use Autolayout basically for the first time. And I'm not understanding why the constraints aren't working in this case. Here's the code that I'm using to resize the UIImageView after I've loaded the proper image into it.

// Scale the image view and return it.
- (UIImageView *) scaleImageViewForScreenWidth:(UIImageView *)imageView {
    UIImage *imgFromView = [imageView image];
    CGRect newFrame = CGRectMake(0, 0, imgFromView.size.width, imgFromView.size.height);

    float imgFactor = newFrame.size.height / newFrame.size.width;
    newFrame.size.width = [[UIScreen mainScreen] bounds].size.width;
    newFrame.size.height = newFrame.size.width * imgFactor;
    [imageView setFrame:newFrame];
    return imageView;
}

As far as constraints are concerned. I'm trying to attach a Label and UIImageView with a shadow to the bottom of the main imageview. Here are the constraints that I'm applying to a bottom shadow in the imageview. The bottom shadow constraints are:

Height Equals:  83
Align Bottom to: Background Image View
LeadingSpace to: Table View Cell

I'm not getting what I want though. Any ideas? I feel like I'm fighting autolayout.


回答1:


As requested! Here's some guidelines for using Autolayout that should help a lot.

The first thing is that the ideal approach is for you to set things up in Interface Builder so that no further programming is required. So, if - for example - the bounds of your view change, then your view will adjust itself automatically as required.

If that doesn't do the business, then you may have to update the constraints programmatically. Now, the golden rules as I mentioned is that you update the constraints. Resist the temptation to update the underlying UIView frame!

So, you'll do something like:

_myWidthConstraint.constant = 300.f;

The next thing to note is that you should do this in a specific place, and that is in your UIView subclass method updateConstraints:

- (void)updateConstraints
{
    [super updateConstraints];
    _myWidthConstraint.constant = 300.f;
}

How do you trigger that? By invoking:

    [self setNeedsUpdateConstraints];

Hope this helps! For further info check Ole Begemann's excellent article 10 Things You Need To Know About Cocoa Autolayout.

Don't forget the WWDC videos. These are essential!

Also, now there's a book iOS Auto Layout Demystified . Although I've bought it, I haven't had a chance to read it yet. It does look pretty good though.




回答2:


I was also facing the same issue. the image inside the imageview was too big for the imageview and the contentMode was set to AspectFill.

I solved this by unchecking 'Autoresize Subviews".



来源:https://stackoverflow.com/questions/17299223/autolayout-uiimageview-with-programatic-re-size-not-following-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!