2016-09-16 72 views
1
func setupView() 
{ 
    self.blueView = UIView() 
    self.blueView?.backgroundColor = UIColor.blueColor() 
    self.blueView?.translatesAutoresizingMaskIntoConstraints = false 

    self.addSubview(self.blueView!) 

    let blueViewCenterXConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 0.6, constant: 0) 

    let blueViewCenterYConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0) 

    let blueViewWidthConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.6, constant: 150) 

    let blueViewHeightConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150) 

    self.addConstraints([blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint]) 


    self.redView = UIView() 
    self.redView?.backgroundColor = UIColor.redColor() 
    self.redView?.translatesAutoresizingMaskIntoConstraints = false 

    self.addSubview(self.redView!) 

    let redViewCenterXConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.TrailingMargin , multiplier: 0.5, constant: 0) 

    let redViewCenterYConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Top, multiplier: 0.5, constant: 0) 

    let redViewWidthConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Width, multiplier: 0.5, constant: 150) 

    let redViewHeightConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Height, multiplier: 0.5, constant: 150) 

    self.addConstraints([redViewCenterXConstraint,redViewCenterYConstraint,redViewWidthConstraint,redViewHeightConstraint]) 
} 

我已應用上述代碼創建和設置UITextView for Universal App的自動佈局約束。我想把這兩個UITextView放在彼此的每個設備之間,它們之間的空間爲10,水平居中,垂直居中。請有任何人感謝修復上述問題,這對我很有幫助。如何以編程方式爲UITextView設置自動佈局約束對於通用應用程序使用swift

回答

0

我已經包含了一個使用你的代碼的解決方案,但也想告訴你如何使用constraintWithVisualFormat來完成,因爲我認爲它更容易。

爲您解決的代碼: 請注意,寬度在此設置爲固定值。不知道這是否真的是你想要的。

func setupView() 
{ 
    self.blueView = UIView() 
    self.blueView?.backgroundColor = UIColor.blueColor() 
    self.blueView?.translatesAutoresizingMaskIntoConstraints = false 

    self.view.addSubview(self.blueView!) 

    let blueViewCenterXConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: -5) 

    let blueViewCenterYConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0) 

    let blueViewWidthConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 150) 

    let blueViewHeightConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150) 

    self.view.addConstraints([blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint]) 


    self.redView = UIView() 
    self.redView?.backgroundColor = UIColor.redColor() 
    self.redView?.translatesAutoresizingMaskIntoConstraints = false 

    self.view.addSubview(self.redView!) 

    let redViewCenterXConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Trailing , multiplier: 1, constant: 10) 

    let redViewCenterYConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 

    let redViewWidthConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 1) 

    let redViewHeightConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 1) 

    self.view.addConstraints([redViewCenterXConstraint,redViewCenterYConstraint,redViewWidthConstraint,redViewHeightConstraint]) 
} 

使用解決方案的視覺限制

 let views = ["redView": redView!, "blueView": blueView!] 

    //setup the horizontal constraints to have 15 leading and trailing constraints, equal widths for blueView and redView and 10 spacing in between. 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[blueView(==redView)]-10-[redView]-15-|", options: .AlignAllCenterY, metrics: nil, views: views)) 

    //set the height of the two views 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[blueView(150)]", options: .AlignAllCenterX, metrics: nil, views: views)) 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[redView(150)]", options: .AlignAllCenterX, metrics: nil, views: views)) 

    //align the two views relative to the centre of the superview. 
    blueView!.centerYAnchor.constraintEqualToAnchor(view!.centerYAnchor, constant: 0).active = true 
    redView!.centerYAnchor.constraintEqualToAnchor(blueView!.centerYAnchor).active = true 

爲您更換例如與self.viewself

+0

使用視覺約束的解決方案幫助我獲得了很好的解決方案。真的,我感謝你的寶貴努力..! –

4

我已經採取了尋找自由你previous question,此外我已經使用NSLayoutAnchors(描述爲here),因爲我認爲它們更易於閱讀。

基於上述,本UIView子類:

import UIKit 

class UIViewUsingTextField: UIView { 

    let width: CGFloat = 150.0 

    var blueview = UIView() 
    var redview = UIView() 

    init() { 
     super.init(frame: CGRect.zero) 
     self.backgroundColor = UIColor.whiteColor() 
     self.setupView() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     setupView() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupView() 
    } 

    func setupView() { 
     //add blue view 
     blueview.backgroundColor = UIColor.blueColor() 
     blueview.translatesAutoresizingMaskIntoConstraints = false 
     addSubview(blueview) 

     //center the blue view and move it 1/2 view width to the left 
     blueview.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor, constant: -(width/2)).active = true 
     blueview.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true 
     blueview.widthAnchor.constraintEqualToConstant(width).active = true 
     blueview.heightAnchor.constraintEqualToConstant(width).active = true 

     //add red view 
     redview.backgroundColor = UIColor.redColor() 
     redview.translatesAutoresizingMaskIntoConstraints = false 
     addSubview(redview) 

     //place red view 10 px to the right of blue view 
     redview.leadingAnchor.constraintEqualToAnchor(blueview.trailingAnchor, constant: 10.0).active = true 
     redview.centerYAnchor.constraintEqualToAnchor(blueview.centerYAnchor).active = true 
     redview.widthAnchor.constraintEqualToAnchor(blueview.widthAnchor).active = true 
     redview.heightAnchor.constraintEqualToAnchor(blueview.heightAnchor).active = true 
    }  
} 

給了我這樣的佈局

portrait

landscape

希望這類似於你是什麼之後。

相關問題