Ios 为什么在Swift中ipad的横向模式中文本字段中的水平线相互冲突?

Ios 为什么在Swift中ipad的横向模式中文本字段中的水平线相互冲突?,ios,swift,ipad,landscape-portrait,viewdidlayoutsubviews,Ios,Swift,Ipad,Landscape Portrait,Viewdidlayoutsubviews,我在Xcode 9,iOS 11中使用这段代码 var boolName: Bool = false override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) if boolName == true { self.designTextField()

我在Xcode 9,iOS 11中使用这段代码

var boolName: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    if boolName == true {
        self.designTextField()
        self.view.layoutIfNeeded()
    }
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if boolName == false {

        self.designTextField()
        boolName = true
    }
    self.view.layoutIfNeeded()
}

func designTextField() {
    //Set the horizontal line in bottom of text field
    nameLayer.frame = CGRect(x: 0, y: self.tfName.bounds.size.height, width: self.tfName.bounds.size.width, height: 1)
    nameLayer.backgroundColor = UIColor.lightGray.cgColor
    tfName.layer.addSublayer(nameLayer)

    //Set the horizontal line in bottom of text field
    phoneLayer.frame = CGRect(x: 0, y: self.tfPhone.bounds.size.height, width: self.tfPhone.bounds.size.width, height: 1)
    phoneLayer.backgroundColor = UIColor.lightGray.cgColor
    tfPhone.layer.addSublayer(phoneLayer)

    //Set the horizontal line in bottom of text field
    emailLayer.frame = CGRect(x: 0, y: self.tfEmail.bounds.size.height, width: self.tfEmail.bounds.size.width, height: 1)
    emailLayer.backgroundColor = UIColor.lightGray.cgColor
    tfEmail.layer.addSublayer(emailLayer)
}
在iPad上使用该代码并进行测试时。问题是,当我以横向模式旋转iPad时,文本字段的水平线(在底部)相互冲突


有人能帮上忙吗?

Case
如果boolName==false
在横向模式下停止重画,请将其删除。在method designTextField中,设置行层的名称,然后您可以获取并更新它们

let kLineName = "nameLine"

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.designTextField()
}

func designTextField()
{
    //Set the horizontal line in bottom of text field
    if let index = tfName.layer.sublayers?.index(where: { (layer) -> Bool in
        return layer.name == kLineName
    }) {
        //Update line's frame if it's existed.
        let nameLayer = tfName.layer.sublayers![index]
        nameLayer.frame = CGRect(x: 0, y: self.tfName.bounds.size.height, width: self.tfName.bounds.size.width, height: 1)
    }
    else{
        //Add layer if it's not existed.
        nameLayer.frame = CGRect(x: 0, y: self.tfName.bounds.size.height, width: self.tfName.bounds.size.width, height: 1)
        nameLayer.backgroundColor = UIColor.lightGray.cgColor
        nameLayer.name = kLineName
        tfName.layer.addSublayer(nameLayer)
    }

    //Same to the phoneLayer and emailLayer ......

}

检查iPad横向尺寸类的尾随约束。但当我点击该文本字段时,该行背景颜色将保持不变(我想更改此蓝色),您可以使用相同的方法获取图层并在UITextFieldDelegate方法
textFieldDidBeginEditing
中更改颜色。但这不会更改颜色