简体   繁体   English

如何使用视觉格式语言在Swift中设置约束?

[英]How to use Visual Format Language to set constraints in Swift?

I am having a View Controller which has a Nav Bar, I am trying to implement a Menu Bar right below the Nav Bar for which I have a separate class file(UIView). 我有一个带有导航栏的View Controller,我试图在导航栏正下方实现一个菜单栏,为此我有一个单独的类文件(UIView)。 After adding the Menu Bar, I want to add an UIImageView exactly at the vertical centre of the Menu Bar View and not the Super View and have height as same as the Menu Bar View. 添加菜单栏后,我想在菜单栏视图的垂直中心而不是超级视图的垂直中心添加一个UIImageView,并且其高度与菜单栏视图相同。 I am using Visual Format Language. 我正在使用视觉格式语言。 I am not really sure how to mention the view's name and how to constraint the image's height and place it in the centre of a named View. 我不太确定如何提及视图的名称以及如何限制图像的高度并将其放置在命名视图的中心。 Can someone help me ? 有人能帮我吗 ?

Below are code snippets... 以下是代码片段...

//MenuBar.swift //MenuBar.swift

class MenuBar: UIView {


override init(frame: CGRect) {

    super.init(frame: frame)
    print("created") 

    let imageLogo = UIImage(named: "Logo")
    let imageView = UIImageView(image: imageLogo)
    self.addSubview(imageView)
    self.addConstraintWithFormat("H:|[v0]|", views: imageView)
    self.addConstraintWithFormat("V:|[v0(100)]|", views: imageView)
    backgroundColor = UIColor.red
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

//UIView Extension for the func addConstraintWithFormat(- format:, views:) // UIView扩展的func addConstraintWithFormat(- format:, views:)

extension UIView{
func addConstraintWithFormat(_ format : String, views : UIView...) {

    var viewsDictionary = [String : UIView]()

    for(index, view) in views.enumerated(){
        let key = "v\(index)"
        view.translatesAutoresizingMaskIntoConstraints = false
        viewsDictionary[key] = view
    }
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary))

}

Visual Format Language allows you to apply programmatic constraints using visual syntax strings. 视觉格式语言允许您使用视觉语法字符串应用程序约束。 As per the Apples Documentation , the idea is the text visually matches the layout. 根据Apples文档 ,其想法是文本在视觉上与布局匹配。

Let's break down the syntax part for better understanding: 让我们分解语法部分以更好地理解:

H: (Horizontal) //horizontal direction
V: (Vertical) //vertical direction
| (pipe) //superview
- (dash) //standard spacing (generally 8 points)
[] (brackets) //name of the object (uilabel, unbutton, uiview, etc.)
() (parentheses) //size of the object
== equal widths //can be omitted
-16- non standard spacing (16 points)
<= less than or equal to
>= greater than or equal to
@250 priority of the constraint //can have any value between 0 and 1000

Now, in order to apply constraint to a view using visual format language, fir we need to make translatesAutoresizingMaskIntoConstraints false for the view, on which we are going to apply constraints: 现在,为了使用视觉格式语言将约束应用于视图,首先,我们需要将视图的translatesAutoresizingMaskIntoConstraints false,然后在该视图上应用约束:

imageView.translatesAutoresizingMaskIntoConstraints = false

then we need to prepare a dictionary for all the views, which are to be used in VFL like: 那么我们需要为所有视图准备一个字典,这些视图将在VFL中使用,例如:

let viewDictionary = NSDictionaryOfVariableBindings(imageView)

then make horizontal and vertical constraints using Visual Format String as per the rules explained above: 然后根据上述规则使用Visual Format String进行水平和垂直约束:

let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[imageView]-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary)
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-[imageView(100)]-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary)

Now, add these constants to your superview like: 现在,将这些常量添加到您的超级视图中,如下所示:

view.addConstraints(horizontalConstraints)
view.addConstarints(verticalConstraints)

PS: If you want to make view's width/height dynamic, you need to create a matrics dictionary, pass it in metrics: instead of setting it nil and then using the appropriate keyname for the value. PS:如果要使视图的宽度/高度动态变化,则需要创建一个矩阵字典,将其传递到metrics:而不是将其设置为nil ,然后使用适当的键名作为值。 For example: 例如:

let metricDict = ["viewHeight":300]
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-[imageView(viewHeight)]-|", options: NSLayoutConstraint.FormatOptions(), metrics: metricDict, views: viewsDictionary)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM