2

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

4

1 回答 1

0

当您以编程方式创建视图时,您必须记住设置translatesAutoresizingMaskIntoConstraints为 false 否则自动布局将不会像您预期的那样运行。

于 2016-11-14T00:00:02.620 回答