我正在尝试在iOS 10中添加一个自定义通知,它利用了强制按富通知功能。我想以编程方式将一个视图添加到继承自UNNotificationContentExtension的视图控制器的中心,但我得到的只是一个白屏。当我给它一个这样的框架时,内容就会被添加进来:
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = UIColor.red
myView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.view.addSubview(myView)
}
func didReceive(_ notification: UNNotification) {
}
}它看起来像这样:

但是,当我尝试使用以下代码以编程方式(通过PureLayout包装器)使用AutoLayout时:
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = UIColor.red
self.view.addSubview(myView)
myView.autoPinEdge(toSuperviewEdge: .top)
myView.autoPinEdge(toSuperviewEdge: .left)
myView.autoMatch(.width, to: .width, of: self.view)
myView.autoMatch(.height, to: .height, of: self.view)
}
func didReceive(_ notification: UNNotification) {
}
}结果是这样的:

什么会导致AutoLayout在这个视图控制器中不能工作?我也用接口构建器测试过它,所以我很困惑。
发布于 2016-11-14 08:00:03
当您以编程方式创建视图时,您必须记住将translatesAutoresizingMaskIntoConstraints设置为false,否则自动布局的行为将不会像您预期的那样。
https://stackoverflow.com/questions/40472293
复制相似问题