label showing top of screen instead of being on the inputAccessoryView

后端 未结 2 447
情歌与酒
情歌与酒 2021-01-24 03:49

Here is my code:

 var messageView : UITextView = {
        var textView = UITextView()
        textView.text = \"   Add your message here\"
        textView.text         


        
相关标签:
2条回答
  • 2021-01-24 04:35

    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
    
    0 讨论(0)
  • 2021-01-24 04:36

    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
    
    0 讨论(0)
提交回复
热议问题