本文介绍了标签显示在屏幕顶部,而不是显示在inputAccessoryView上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 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
    }()

我正在尝试将inputAccessoryView添加到TextView的键盘上.我的inputAccessoryView必须有一个标签,上面写着您的字符数限制为100" ...

I'm trying to add an inputAccessoryView to my TextView's keyboard.My inputAccessoryView must have a label saying "You have a 100 character limit"...

但是我目前的结果是:

蓝色文本...恰好是我要在inputAccessoryView中使用的标签,但是它在屏幕顶部...

The text in the blue...is exactly the label I want to be in the inputAccessoryView, but it's on the top of my screen...

推荐答案

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

You need to set translatesAutoresizingMaskIntoConstraints on the label to false and isActive to true on the constraints. Basically your constrains code should look like this:

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

这篇关于标签显示在屏幕顶部,而不是显示在inputAccessoryView上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 18:17