objective-c - 使用自动布局自定义纵横比

假设我有一个 UIView,其中包含一个必须为 4:3 的子 UIView。我想在代码中使用 AutoLayout 来解决这个问题。

我一直在努力使用 AutoLayout,但我还没有弄清楚如何做到这一点。

你知道如何解决这个问题吗?

非常感谢。

我附上一张图片来更好地解释我的意思。 http://d.pr/i/d0Oc

最佳答案

我想通了。

//Given a childView... 
NSLayoutConstraint *constraint =[NSLayoutConstraint
                           constraintWithItem:childView
                           attribute:NSLayoutAttributeWidth
                           relatedBy:NSLayoutRelationEqual
                           toItem:childView
                           attribute:NSLayoutAttributeHeight
                           multiplier:4.0/3.0 //Aspect ratio: 4*height = 3*width
                           constant:0.0f];
            [childView addConstraint:constraint];

https://stackoverflow.com/questions/12510705/

相关文章:

iphone - 如何使用第一个字符作为部分名称

ios - 计算两个 CLLocationCoordinate2D 之间的方位

iphone - 什么是-[NSString sizeWithFont :forWidth:line

objective-c - Objective C 中的多个委托(delegate)

iphone - animateWithDuration :animations: block ma

iphone - 在 Objective-C 中随机化 NSArray 的规范方法

iphone - UITableView/UITableViewCell 点击事件响应?

ios - 在objective-c中导入swift类,-Swift.h文件未找

objective-c - 适合 UILabel 中的文本

objective-c - 如何从 C 方法调用 Objective-C 方法?