对 viewController 方法的奇怪行为感到非常困惑
getting highly confused by odd behaviour of viewController Method
我已经重写了一个方法
override func viewDidLayoutSubviews() {
// creating bottom line for textField
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
border.borderWidth = width
emailTextField.layer.addSublayer(border)
emailTextField.layer.masksToBounds = true
}
现在发生的事情是,当我 运行 我的应用程序在 Iphone 6、6+ 时一切正常。但是当我 运行 iphone5
(模拟器 + 真实设备)viewDidLayoutSubViews
上的相同代码被无限次调用时,我的应用程序变得无响应。我通过使用 bool 变量解决了这个问题。但我不明白为什么会这样。那么有人可以向我解释一下吗?
谢谢:-)
只需更换
emailTextField.layer.addSublayer(border)
有了这个:
emailTextField.addSublayer(border)
-> 您想将子层添加到视图中 - 因为您也想对其应用 masksToBounds
属性。
正如文档所说,viewDidLayoutSubviews
方法需要更改布局
Called to notify the view controller that its view has just laid out
its subviews. When the bounds change for a view controller's view,
the view adjusts the positions of its subviews and then the system
calls this method. However, this method being called does not indicate
that the individual layouts of the view's subviews have been
adjusted. Each subview is responsible for adjusting its own layout.
如果您不使用 xibs
或 Storyboards
,请将您的代码移至 viewDidLoad
方法或 loadView
override func viewDidLoad() {
// creating bottom line for textField
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
border.borderWidth = width
emailTextField.layer.addSublayer(border)
emailTextField.layer.masksToBounds = true
}
如果您想更改 border.frame
,您可以将其设置为 class 变量并在 viewDidLayoutSubviews
方法中进行更改
override func viewDidLayoutSubviews() {
self.border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
self.border.borderWidth = width
}
将您的实现移到 viewDidLayoutSubviews 中。我遇到了同样的问题。希望有用。
override func viewDidLayoutSubviews()
{
//Your CODE
}
我已经重写了一个方法
override func viewDidLayoutSubviews() {
// creating bottom line for textField
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
border.borderWidth = width
emailTextField.layer.addSublayer(border)
emailTextField.layer.masksToBounds = true
}
现在发生的事情是,当我 运行 我的应用程序在 Iphone 6、6+ 时一切正常。但是当我 运行 iphone5
(模拟器 + 真实设备)viewDidLayoutSubViews
上的相同代码被无限次调用时,我的应用程序变得无响应。我通过使用 bool 变量解决了这个问题。但我不明白为什么会这样。那么有人可以向我解释一下吗?
谢谢:-)
只需更换
emailTextField.layer.addSublayer(border)
有了这个:
emailTextField.addSublayer(border)
-> 您想将子层添加到视图中 - 因为您也想对其应用 masksToBounds
属性。
正如文档所说,viewDidLayoutSubviews
方法需要更改布局
Called to notify the view controller that its view has just laid out its subviews. When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
如果您不使用 xibs
或 Storyboards
viewDidLoad
方法或 loadView
override func viewDidLoad() {
// creating bottom line for textField
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
border.borderWidth = width
emailTextField.layer.addSublayer(border)
emailTextField.layer.masksToBounds = true
}
如果您想更改 border.frame
,您可以将其设置为 class 变量并在 viewDidLayoutSubviews
方法中进行更改
override func viewDidLayoutSubviews() {
self.border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width: emailTextField.frame.size.width, height: emailTextField.frame.size.height)
self.border.borderWidth = width
}
将您的实现移到 viewDidLayoutSubviews 中。我遇到了同样的问题。希望有用。
override func viewDidLayoutSubviews()
{
//Your CODE
}