动画时Autolayout重绘

时间:2017-08-06 06:56:09

标签: ios swift3 uiview autolayout ios-autolayout

所以我有一个(ViewController)和一个 SubView (Slider),其中SubView有一个datePicker和一个按钮。

Subview本身是一个使用自动布局将datePicker和Button放在适当位置的类,因此在调用此视图时,datePicker和按钮通常具有不同屏幕尺寸的iPhone。

问题:当从父视图调用此子视图(滑块)时,似乎此视图中的组件在运行时从左到右绘制,在我看来它看起来很糟糕。

下面是我的一些代码和**我已经评论了几个组件部分,首先开始解决按钮问题**但还没有成功。

import UIKit

class Slider: UIView {

var title : UILabel = UILabel()
var testPicker : UIDatePicker = UIDatePicker()
var txtfield : UITextField = UITextField()

var SVSetButton: UIButton = UIButton()


   override init(frame: CGRect) {

    super.init(frame: frame)

    self.backgroundColor = UIColor.white

    //self.title.frame = CGRect(x: 50, y: 30, width: 200, height: 40)
    self.title.translatesAutoresizingMaskIntoConstraints = false
    self.title.text = "Child Object"
    self.title.textAlignment = .center
    self.title.font = UIFont(name: "avenir", size: 26.0)

    self.txtfield.frame = CGRect(x: 0, y: 20, width:      self.self.bounds.width, height: 35)
    self.txtfield.placeholder = "Text"

    self.testPicker.translatesAutoresizingMaskIntoConstraints = false
    self.testPicker.backgroundColor = UIColor.red

    self.SVSetButton.translatesAutoresizingMaskIntoConstraints = false
    self.SVSetButton.setTitle("Done", for: .normal)
    self.SVSetButton.backgroundColor = UIColor.alizarinColor()
    //self.SVSetButton.addTarget(self, action:  #selector(DateView.SaveRemindingData), for: .touchUpInside)

    //self.addSubview(testPicker)
    self.addSubview(SVSetButton)
//          self.addSubview(txtfield)


}


override func willMove(toSuperview newSuperview: UIView?) {
    self.title.layoutIfNeeded()
    self.SVSetButton.layoutIfNeeded()
}



override func layoutSubviews() {

    super.layoutSubviews()

    //        let centerX = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
    //        
    //        
    //        let centerBottom = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: -10)
    //        
    //        let height = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)
    //        
    //        let width = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.self, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -15)


    // MARK: - Date picker test



       //        let pcenterX = NSLayoutConstraint(item: self.testPicker, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
      //        
      //        
      //        let pcenterBottom = NSLayoutConstraint(item:     self.testPicker, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.SVSetButton, attribute: NSLayoutAttribute.top, multiplier: 1, constant: -10)
      //        
      //        let pheight = NSLayoutConstraint(item: self.testPicker,  attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 160)
      //        
      //        let pwidth = NSLayoutConstraint(item: self.testPicker, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.self, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -15)



    let buttonCenterX = NSLayoutConstraint(item: self.SVSetButton, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)


    let buttonCenterBottom = NSLayoutConstraint(item: self.SVSetButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.self, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -10)

    let buttonHeight = NSLayoutConstraint(item: self.SVSetButton, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 50)

    let buttonWidth = NSLayoutConstraint(item: self.SVSetButton, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.self, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -15)



    self.addConstraints([/*pcenterX, pcenterBottom, pheight, pwidth,*/ buttonCenterX, buttonCenterBottom, buttonHeight, buttonWidth])


}

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

}

以下是使用自动布局在父视图中调用此滑块的代码段。

ViewController(父级)

    func invokeRemindForm(){

    self.card.textpad.resignFirstResponder()

    self.sliderBackground.addGestureRecognizer(dismissTap)


    slideView = Slider(frame: CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/2))

    slideView?.translatesAutoresizingMaskIntoConstraints = false
    sliderOpen = true

    if sliderOpen {
        self.remindButton.isEnabled = false
    }

    if let slider = slideView {

       // view.addSubview(slider)
        view.addSubview(sliderBackground)
        self.sliderBackground.addSubview(slider)
        sliderBackground.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)

        // This value may  not be working entirely
        sliderBackground.alpha = 1.0
        sliderBackground.backgroundColor = UIColor.black.withAlphaComponent(0.7)


        //  Autolayout
        let leading = NSLayoutConstraint(item: self.slideView!, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0)
        let trailing = NSLayoutConstraint(item: self.slideView!, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: 0)
        let bottom = NSLayoutConstraint(item: self.slideView!, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0)
        let height = NSLayoutConstraint(item: self.slideView!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.height/2)

        self.slideView?.layoutIfNeeded()
        self.view.addConstraints([leading, trailing, bottom, height])

    }

    UIView.animate(withDuration: 0.3, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
        self.view.layoutIfNeeded()

    })

}

我做错了什么?我似乎认为滑块中的自动布局应该包含所有组件,并且在调用视图时不应该自己绘制。有什么建议?

0 个答案:

没有答案