为什么在 ipad 和 Swift 的横向模式下,文本字段中的水平线会发生冲突?
Why the Horizontal line in text fields are conflicting in Landscape mode in ipad in Swift?
我在 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 时,文本字段的水平线(底部)发生冲突。
有人能帮忙吗?
案例if boolName == false
停止横向重绘,移除。并在 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 ......
}
我在 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 时,文本字段的水平线(底部)发生冲突。
有人能帮忙吗?
案例if boolName == false
停止横向重绘,移除。并在 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 ......
}