我已经为我的视图控制器使用了自动布局.我已经在约束中设置了V和H位置,但我想知道当按钮变为5s、6和6Plus时,我如何才能增加按钮大小.这是我为登录按钮添加约束的方式:

NSArray *btncon_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[btnLogin(40)]" options:0 metrics:nil views:viewsDictionary];
[btnLogin addConstraints:btncon_V];

NSArray *btncon_POS_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[btnLogin]-100-|" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:btncon_POS_H];


NSArray *btncon_POS_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[Title]-130-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-10-[btnLogin]" options:0 metrics:nil views:viewsDictionary];

[self.view addConstraints:btncon_POS_V];

但我的问题是,虽然它管理左右两侧的间隙,但由于高度固定,iPhone 6和6 Plus的间隙越来越大.如何根据屏幕大小增加尺寸?我认为这可能是纵横比,但如何在代码中设置纵横比约束?

推荐答案

这样地.试试看.

[self.yourview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.yourview addConstraint:[NSLayoutConstraint
                                  constraintWithItem:self.yourview
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.yourview
                                  attribute:NSLayoutAttributeWidth
                                  multiplier:(self.yourview.frame.size.height / self.yourview.frame.size.width)
                                  constant:0]];

或者在(self.yourview.frame.size.height / self.yourview.frame.size.width)的位置,可以使用任何浮点值.

Swift 3.0 -

self.yourview!.translatesAutoresizingMaskIntoConstraints = false
self.yourview!.addConstraint(NSLayoutConstraint(item: self.yourview!,
                                          attribute: NSLayoutAttribute.height,
                                          relatedBy: NSLayoutRelation.equal,
                                          toItem: self.yourview!,
                                          attribute: NSLayoutAttribute.width,
                                          multiplier: self.yourview.frame.size.height / self.yourview.frame.size.width,
                                          constant: 0))

Ios相关问答推荐

Firebase SDK 10+—无法安装软件包

不使用iOS 17修改器修改SWIFT用户界面视图

在相机动作之前快速防止场景视点重置

SwiftUI Divider过大了其父视图

有没有办法将滚动视图RTL(阿拉伯语)与Ltr动画一起使用?

在 SwiftUI 中以编程方式插入内嵌图像

如何将 AppIntent 添加到应用程序目标并在交互式小部件中使用它

核心媒体基础知识,了解 CMSampleBuffer、CMBlockBuffer 和 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer

Swift Combine:prepend() before share() 阻止初始接收器执行

@MainActor 类的扩展是主要演员吗?

如何让替换视图淡入旧视图而不是交叉淡入淡出?

在 Swift 中映射 MySQL 数据

在 SwiftUI 中打开 PDF

如何在 Swift 3 中根据 UIImage 的大小/比率调整 UIImageView 的大小?

SwiftUI 视图 - viewDidLoad()?

'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据

UIView 动画与 CALayers

在 Xcode 4 中将静态库链接到 iOS 项目

Sierra 中的安全/协同设计: keys 串忽略访问控制设置和 UI 提示权限

Swift 中的日期到毫秒和回溯到日期