简体   繁体   English

如何将视图高度设置为等于其宽度

[英]How to set a view height to be equal to its width

I created a UIView, then I added a constrain on the uiview that its width is equal to its superview width. 我创建了一个UIView,然后我在uiview上添加了一个约束,它的宽度等于它的superview宽度。 Then I want to make this uiview's height to be equal to it's width. 然后我想让这个uiview的高度等于它的宽度。 I don't know how to add this kind of constrain. 我不知道如何添加这种约束。 I can see that I can add a height constrain on this view but how to set its value to be equal to its width? 我可以看到我可以在此视图上添加高度约束,但如何将其值设置为等于其宽度?

Add the Aspect Ratio constraint to your view 将Aspect Ratio约束添加到视图中 在此输入图像描述

Try this: 尝试这个:

yourView.translatesAutoresizingMaskIntoConstraints = false
yourView.addConstraint(NSLayoutConstraint(item: yourView, attribute: .Height, relatedBy: .Equal, toItem: yourView, attribute: .Equal, multiplier: 1, constant: yourWidthHere))

Or you can drag IBOutlet of your Width Constraint and Height Constraint to your ViewController: 或者,您可以将Width Constraint和Height Constraint的IBOutlet拖到ViewController:

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!

and set the height equal to width: 并将高度设置为宽度:

heightConstraint.constant = widthConstraint.constant

you can set a view height to be equal to its width either from storyboard or programmatically for this you need to add the aspect ratio constraint on a view with multiplier 1. 您可以从storyboard或以programmatically将视图高度设置为等于其宽度,因此您需要在具有乘数1的视图上添加aspect ratio约束。

From storyboard you can add the aspect ratio constraint with the help of Pin Tools in storyboard 在故事板中,您可以在故事板中借助Pin Tools添加aspect ratio约束

By programmatically you can try below code, It is working fine. 通过编程方式,您可以尝试下面的代码,它工作正常。

override func viewDidLoad() {
    super.viewDidLoad()

    let myView = UIView()
    myView.backgroundColor = UIColor.redColor()
    self.view.addSubview(myView)
    myView.translatesAutoresizingMaskIntoConstraints = false

    // here I'm setting the CenterX & CenterY constraint equal to its superview CenterX & CenterY
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))

    // here I'm setting the width constraint equal to its superview width
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0))

    // here I'm setting the aspect ratio constraint to equal width or height of myView
    // since  aspect ratio constraint belongs to self width or height so add this constraints it self(i.e. myView) but not in its superView
    myView.addConstraint(NSLayoutConstraint(item : myView, attribute: .Width, relatedBy: .Equal, toItem: myView, attribute: .Height, multiplier: 1, constant: 0))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置UIImage的高度等于其宽度 - Set UIImage's height equal to its width 如何在 StackView 中设置等宽或等高? - How to Set Equal Width or Equal Height in StackView? iOS - 如何将视图宽度设置为等于TableView分隔符宽度 - iOS - How to set View width equal to TableView Separator width 如何使用特定的宽度和高度视图设置约束 - How to set constraint with a specific width and height view 具有固定宽度的UIImageView:如何设置其高度以保持其宽高? - UIImageView with fixed width: how to set its height to keep its aspect? 使用autolayout可视化格式语言可以将视图的宽度设置为等于同一视图的高度吗? - Using the autolayout visual format language is it possible to set the width of a view equal to the height of the same view? iOS图像视图 - 高度等于宽度的大小 - iOS Image View - Height equal to the size of width 如何根据子视图的高度设置堆栈视图的高度? - How to set stack view height according to its subviews height? 如何在自定义单元格中使UIImage的宽度等于视图的宽度和比例高度 - How to make UIImage width be equal to view's width and proportional height inside a custom cell 如何在瀑布视图中自动设置图像的宽度和高度? - How to set image width and height automatically in a waterfall view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM