自动布局 UIImageView 与编程重新大小不遵循约束 [英] Autolayout UIImageView with programatic re-size not following constraints

查看:21
本文介绍了自动布局 UIImageView 与编程重新大小不遵循约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉打扰了,但我正在尝试延迟加载几个 imageView,然后根据 UITableView 中的内容按比例调整其大小.我也在尝试(可能是不明智的?)基本上是第一次使用自动布局.而且我不明白为什么约束在这种情况下不起作用.这是我在加载正确的图像后用来调整 UIImageView 大小的代码.

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;
}

就约束而言.我正在尝试将带有阴影的标签和 UIImageView 附加到主图像视图的底部.这是我应用于图像视图中底部阴影的约束.底部阴影约束是:

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.

推荐答案

按要求!下面是一些使用 Autolayout 的指南,应该会有很大帮助.

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

首先,理想的方法是让您在 Interface Builder 中进行设置,这样就不需要进一步的编程.因此,如果 - 例如 - 您的视图边界发生变化,那么您的视图将根据需要自动调整.

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.

如果这不起作用,那么您可能必须以编程方式更新约束.现在,我提到的黄金法则是更新约束.抵制更新底层 UIView 框架的诱惑!

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;

接下来要注意的是,您应该在特定的地方执行此操作,那就是在您的 UIView 子类方法 updateConstraints:

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];

希望这会有所帮助!欲了解更多信息,请查看 Ole Begemann 的优秀文章 关于 Cocoa 自动布局你需要知道的 10 件事.

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

不要忘记 WWDC 视频.这些是必不可少的!

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

另外,现在有一本书 iOS 自动布局揭秘.虽然我已经买了它,但我还没有机会阅读它.不过看起来确实不错.

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.

这篇关于自动布局 UIImageView 与编程重新大小不遵循约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆