我沒有做很多的UI編程,但我有一個UIView,我想UILabel被約束到UIView的左上角,然後有另一個約束到右上角這次。我完全意識到如何在GUI上做到這一點,但我想知道如何純粹通過swift/programatically來實現這一點。多謝你們。以編程方式將約束添加到UIView
2
A
回答
1
這是用NSLayoutConstraint
類完成的。
首先,添加標籤爲子視圖:
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
這是我爲了方便地添加約束子視圖擴展。這裏的主要方法爲你學習是func pin(firstSubview subview1:UIView, firstEdge edge1:NSLayoutAttribute, secondSubview subview2:UIView, secondEdge edge2:NSLayoutAttribute, with constant:Float)
extension UIView {
func pinSubview(_ subview:UIView, toEdge edge:NSLayoutAttribute, withConstant constant:Float) {
self.pinSubviews(self, subview2: subview, toEdge: edge, withConstant: constant)
}
func pinSubviews(_ subview1:UIView, subview2:UIView, toEdge edge:NSLayoutAttribute, withConstant constant:Float) {
pin(firstSubview: subview1, firstEdge: edge, secondSubview: subview2, secondEdge: edge, with: constant)
}
func pin(firstSubview subview1:UIView, firstEdge edge1:NSLayoutAttribute, secondSubview subview2:UIView, secondEdge edge2:NSLayoutAttribute, with constant:Float) {
let constraint = NSLayoutConstraint(item: subview1, attribute: edge1, relatedBy: .equal, toItem: subview2, attribute: edge2, multiplier: 1, constant: CGFloat(constant))
self.addConstraint(constraint)
}
func pinSubview(_ subview:UIView, withHeight height:CGFloat) {
let height = NSLayoutConstraint(item: subview, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: height)
self.addConstraint(height)
}
func pinSubview(_ subview:UIView, withWidth width:CGFloat) {
let width = NSLayoutConstraint(item: subview, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width)
self.addConstraint(width)
}
}
然後你有非常簡單的用法:
self.view.pinSubview(label, toEdge: .left, withConstant: 0)
self.view.pinSubview(label, toEdge: .top, withConstant: 0)
0
看看下面的代碼以供參考以編程方式添加約束在一個視圖
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
//ios9 constraint anchors
//x,y,w,h
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
let sendButton = UIButton(type: .system)
sendButton.setTitle("Send", for: UIControlState())
sendButton.translatesAutoresizingMaskIntoConstraints = false
sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
containerView.addSubview(sendButton)
//x,y,w,h
sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
containerView.addSubview(inputTextField)
//x,y,w,h
inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
0
這是編程式添加約束到UIView的一種方法()
override func viewDidLoad() {
super.viewDidLoad()
let centerView = UIView()
centerView.translatesAutoresizingMaskIntoConstraints = false
centerView.backgroundColor = UIColor.brown
view.addSubview(centerView)
let centerViewhorizontalConstraint = NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
let centerViewverticalConstraint = NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 0.90, constant: 0)
let centerViewwidthConstraint = NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 300)
let centerViewheightConstraint = NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant:300)
NSLayoutConstraint.activate([centerViewhorizontalConstraint, centerViewverticalConstraint, centerViewwidthConstraint, centerViewheightConstraint])
}
相關問題
- 1. 以編程方式添加約束時在UIView中啓用AutoLayout
- 2. 如何以編程方式向編程創建的UIView添加約束?
- 3. 如何使用編程約束將CAGradientLayer添加到UIView
- 4. 瞭解使用約束教程以編程方式創建UIView?
- 5. 在UIView子類中添加編程約束的地方?
- 6. 如何以編程方式將約束添加到Swift 2中的按鈕?
- 7. 以編程方式將UINavigationBar添加到UIView?
- 8. 使用MonoTouch以編程方式將UITextField添加到UIView
- 9. 如何以編程方式從UIView獲取約束
- 10. UIView以編程方式設置約束條件
- 11. 以編程方式創建UIView約束,但錨點未應用
- 12. 以編程方式向UITextField添加約束條件
- 13. MonoTouch以編程方式向UITextField添加約束條件
- 14. 刪除以編程方式添加的約束
- 15. 以編程方式添加約束條件
- 16. 以編程方式添加約束問題
- 17. iOS以編程方式添加約束導致錯誤
- 18. IOS以編程方式在Swift中添加約束條件
- 19. 以編程方式向UIImageView添加約束條件
- 20. 在ConstraintLayout中以編程方式刪除/添加約束條件
- 21. 以編程方式添加約束不工作
- 22. 在iOS上以編程方式添加約束條件
- 23. 無法以編程方式向視頻添加約束
- 24. 使用Swift以編程方式向按鈕添加約束
- 25. 以編程方式快速添加約束
- 26. UIView編程寬度約束
- 27. 以編程方式更新約束?
- 28. 以編程方式實現約束
- 29. 以編程方式設置約束
- 30. 以編程方式添加約束時無法找到超級視圖?
[NSLayoutConstraint](https://developer.apple.com/documentation/appkit/nslayoutconstraint)和[自動佈局指南(https://developer.apple.com/圖書館/內容/文檔/用戶體驗/概念/ AutolayoutPG /以編程方式創建Constraints.html)是你的朋友。 – paulvs