自动布局的UIImageView与程序化再没有大小限制之后 [英] Autolayout UIImageView with programatic re-size not following constraints

查看:182
本文介绍了自动布局的UIImageView与程序化再没有大小限制之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉打扰,但我想偷懒加载多个imageViews,然后按比例调整它在一个UITableView的内容。我也想(也许是不明智?)基本上使用自动布局的第一次。而且我不理解为什么约束不会在这种情况下工作。这里的code,我使用调整大小的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阴影主的ImageView的底部。下面是我申请到了一个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.

推荐答案

根据要求!下面是使用自动布局的一些准则应该有很大的帮助。

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

希望这有助于!欲了解更多信息检查奥莱Begemann的优秀文章 10件事情你需要了解可可自动布局

不要忘了WWDC的视频。这些都是必不可少的!

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

此外,现在有一本书 iOS的自动布局揭秘。虽然我已经买了它,我还没有机会读到它。它看起来pretty虽然很好。

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天全站免登陆