如何将乘子应用于var底部约束

时间:2019-12-08 03:40:02

标签: swift uiview nslayoutconstraint var layoutmargins

我下面的代码产生下面的图像。

enter image description here

我想要的是 var lastPathComponent = yourFileURL.lastPathComponent if lastPathComponent.lowercased().contains("trim") { lastPathComponent = lastPathComponent.replacingOccurrences(of: "trim.", with: "") try? yourFileURL.setResourceValue(lastPathComponent, forKey: URLResourceKey.nameKey) // considering your fileURL is an nsurl } 的高度恰好是视图控制器用户的一半,例如:

UIView

我不知道将其放置在哪里,因为我正在为每一面创建一个变量。

constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, multiplier: 1/2)

1 个答案:

答案 0 :(得分:1)

您应该使用heightConstraint而不是bottomConstraint来完成此操作。试试下面的代码段。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    var myView = UIView()
    var topConstraint: NSLayoutConstraint!
    var heightConstraint: NSLayoutConstraint!
    var leadingConstraint: NSLayoutConstraint!
    var trailingConstraint: NSLayoutConstraint!
    var jessicaAlba:Float = 50


    override func viewDidLoad() {
        super.viewDidLoad()

        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.backgroundColor = UIColor.orange
        view.addSubview(myView)

        topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
        topConstraint.isActive = true
        heightConstraint = myView.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.5, constant: CGFloat(-jessicaAlba))
        heightConstraint.isActive = true      

        leadingConstraint = myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
        leadingConstraint.isActive = true
        trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
        trailingConstraint.isActive = true 
    }
}