这是我的代码:

 var messageView : UITextView = {
        var textView = UITextView()
        textView.text = "   Add your message here"
        textView.textColor = UIColor.lightGrayColor()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.backgroundColor = UIColor.lightGrayColor()
        textView.layer.cornerRadius = 3
        textView.clipsToBounds = true
        textView.keyboardAppearance = .Dark
        textView.layer.borderWidth = 1.0
        textView.layer.borderColor = UIColor.lightGrayColor()
        textView.autocorrectionType = .no


        // MARK: Setup accesorryView

        let label = UILabel()
        label.text = "You have a 100 character limit"
        label.translatesAutoresizingMaskIntoConstraints = false

        let accessoryView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 44))
        accessoryView.backgroundColor = UIColor.redColor()

        accessoryView.addSubview(label)

        accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18)
        accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor)

        textView.inputAccessoryView = accessoryView

        return textView
    }()

我正试图在我的TextView键盘上添加一个inputAccessoryView。
我的inputAccessoryView必须有一个标签,上面写着“你有100个字符的限制”。。。
但我现在的结果是:
swift - 标签显示在屏幕顶部,而不是显示在inputAccessoryView上-LMLPHP
蓝色的文本…正是我想放在inputAccessoryView中的标签,但它在我的屏幕顶部。。。

最佳答案

您需要将标签上的translatesAutoresizingMaskIntoConstraints设置为false,并将约束上的isActive设置为true。基本上,约束代码应该如下所示:

accessoryView.leadingAnchor.constraintEqualToAnchor(label.leadingAnchor, constant: 18).isActive = true
accessoryView.centerYAnchor.constraintEqualToAnchor(label.centerYAnchor).isActive = true

关于swift - 标签显示在屏幕顶部,而不是显示在inputAccessoryView上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44008908/

10-10 08:22