当我尝试使用addTextFieldWithConfigurationHandler呈现带有textfeild的alertController时,我收到警告:
2015-12-14 11:03:32.022 TestAlertWithTextField[8455:1172878] the behavior of the UICollectionViewFlowLayout is not defined because: 2015-12-14 11:03:32.022 TestAlertWithTextField[8455:1172878] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 2015-12-14 11:03:32.023 TestAlertWithTextField[8455:1172878] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: . 2015-12-14 11:03:32.023 TestAlertWithTextField[8455:1172878] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
我不知道为什么。
代码:
@IBAction func onButtonClick(sender: UIButton) {
let title = ""
let style = UIAlertControllerStyle.Alert
let alert = UIAlertController(title: title, message: "hello", preferredStyle: style)
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.placeholder = "pass"
})
let cancelTitle = "cancel"
let cancelStyle = UIAlertActionStyle.Cancel
let cancelHandler:((UIAlertAction) -> Void)? = { (action) -> Void in
print("cancelHandler")
}
let cancelAction = UIAlertAction(title: cancelTitle, style: cancelStyle, handler: cancelHandler)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
答案 0 :(得分:0)
您需要致电
alert.view.setNeedsLayout()
之前
self.presentViewController(alert, animated: true, completion: nil)