[英]How do I apply aspect ratio constraints to a custom UICollectionViewCell programmatically?
[英]How can I set aspect ratio constraints programmatically in iOS?
我為我的視圖控制器使用了自動布局。 我已經在約束中設置了 V 和 H 位置,但是我想知道當它更改為 5s、6 和 6 Plus 時如何增加我的按鈕大小。 這是我為登錄按鈕添加約束的方式:
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 中被拉伸。 如何根據屏幕尺寸增加尺寸? 我認為這可能是縱橫比,但是如何在代碼中設置縱橫比約束?
布局錨是以編程方式設置約束的最方便的方法。
假設你想為你的按鈕設置 5:1 的縱橫比,那么你應該使用:
button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true
這是完整的代碼:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .custom)
button.setTitle("Login", for: .normal)
button.backgroundColor = UIColor.darkGray
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
button.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 20.0).isActive = true
button.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: -20.0).isActive = true
button.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -20.0).isActive = true
button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1.0/5.0).isActive = true
}
}
這是使用上面編寫的代碼實現的結果。 您可以看到該按鈕在各種設備上保持 5:1 的縱橫比:
像這樣。 嘗試一次。
[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)
您可以使用任何浮點值。 謝謝。
斯威夫特 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))
斯威夫特 3:
yourView.addConstraint(NSLayoutConstraint(item: yourView,
attribute: .height,
relatedBy: .equal,
toItem: yourView,
attribute: .width,
multiplier: 9.0 / 16.0,
constant: 0))
您可以在設計時在界面生成器中設置“高度約束”。 只需選中“在構建時刪除”,它就會在應用程序運行時刪除。
之后,您可以添加“縱橫比”約束,例如,在 viewDidLoad 方法中。
在“16:9”的 Xamain.iOS 中,它看起來像這樣:
this.ImageOfTheDay.AddConstraint(NSLayoutConstraint.Create(
this.ImageOfTheDay,
NSLayoutAttribute.Height,
NSLayoutRelation.Equal,
this.ImageOfTheDay,
NSLayoutAttribute.Width,
9.0f / 16.0f,
0));
或者,正如Mahesh Agrawal所說:
[self.ImageOfTheDay addConstraint:[NSLayoutConstraint
constraintWithItem:self.ImageOfTheDay
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.ImageOfTheDay
attribute:NSLayoutAttributeWidth
multiplier:(9.0f / 16.0f)
constant:0]];
UIView 上的輔助擴展
extension UIView {
func aspectRation(_ ratio: CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: self, attribute: .width, multiplier: ratio, constant: 0)
}
}
用法是:
view.aspectRation(1.0/1.0).isActive = true
我已經嘗試了上面的所有答案,但沒有奏效,這是我使用 swift 3 的解決方案:
let newConstraint = NSLayoutConstraint(item: yourView, attribute: .width, relatedBy: .equal, toItem: yourView, attribute: .height, multiplier: 560.0/315.0, constant: 0)
yourView.addConstraint(newConstraint)
NSLayoutConstraint.activate([newConstraint])
NSLayoutConstraint.deactivate(yourView.constraints)
yourView.layoutIfNeeded()
Swift 5 解決方案,Xcode 12.4。 需要 ios 9.0+
只需將您的高度限制固定到某個比例:
let aspectRatio: CGFloat = 9 / 16 // Example of ratio you want to apply
yourView.heightAnchor.constraint(equalTo: imageView.widthAnchor,
multiplier: aspectRatio).activate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.