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) {

    }

}

結果は次のとおりです。ここに画像の説明を入力

このView ControllerでAutoLayoutが機能しない原因は何ですか? インターフェイスビルダーでもテストしたので、かなり混乱しています。

4

1 に答える 1

0

プログラムでビューを作成するときは、忘れずtranslatesAutoresizingMaskIntoConstraintsに false に設定する必要があります。そうしないと、自動レイアウトが期待どおりに動作しません。

于 2016-11-14T00:00:02.620 に答える