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

我一直在努力解决自动布局的问题,但我还不知道该怎么做.

你知道怎么解决这个问题吗?

非常感谢你.

我附上一张图片来更好地解释我的意思.

推荐答案

我知道了.

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

Objective-c相关问答推荐

Asihttprequest 61 错误

让 iPhone sleeping

如何更改图像 tintColor

如何获取 NSArray 的最后一个对象

ScrollView 手势识别器吃掉所有的事件

UITableView - Select 了哪一行?

iOS 7 状态栏与 NavigationBar 冲突

未找到 RKObjectMapping.h

Objective-C 中的 alloc、init 和 new

iOS 8 Mapkit Objc 无法获取用户位置

AdMob 因 [GADObjectPrivate changeState:] 崩溃:无法识别的 Select 器

检测 iOS 应用程序是否在调试器中运行

从 iPhone 设备查找当前国家

滑动删除单元格导致 tableView 标题随单元格移动

在 switch 语句中使用 NSString

UIScrollView - 显示滚动条

Objective-C 中的 NSString indexOf

如何删除 UIToolBar 上的顶部边框

将浮点数四舍五入到objective C中的下一个整数?

如何将 printf 与 NSString 一起使用