label showing top of screen instead of being on the inputAccessoryView

烈酒焚心 提交于 2019-12-31 04:16:38

问题


Here is my code:

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

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"...

But my current result is as:

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...


回答1:


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



回答2:


As per my understanding, try this:

Swift 3

let accessoryView = UIView()
let label         = UILabel()
let counterLabel  = UILabel()//This is the counter label

label.text        = "You have a 100 character limit"
counterLabel.text = "100"

accessoryView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44)

accessoryView.backgroundColor = UIColor.red

accessoryView.addSubview(label)
accessoryView.addSubview(counterLabel)

// to setup contraint set below property to false.
label.translatesAutoresizingMaskIntoConstraints = false
counterLabel.translatesAutoresizingMaskIntoConstraints = false

//label constrint with 0 padding from left side. To change padding from left and right side, change the constant value.
accessoryView.leadingAnchor.constraint(equalTo: label.leadingAnchor, constant: 0).isActive = true

accessoryView.centerYAnchor.constraint(equalTo: label.centerYAnchor).isActive = true

//counterl=Label constrint with 0 padding from right side
        accessoryView.trailingAnchor.constraint(equalTo:counterLabel.trailingAnchor, constant: 0).isActive = true

accessoryView.centerYAnchor.constraint(equalTo: counterLabel.centerYAnchor).isActive = true

textView.inputAccessoryView = accessoryView


来源:https://stackoverflow.com/questions/44008908/label-showing-top-of-screen-instead-of-being-on-the-inputaccessoryview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!