我正在尝试在 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 在此视图控制器中不起作用?我也用界面生成器测试过它,所以我很困惑。