How to programmatically add an InputAccessoryView with Autolayout?
Asked Answered
W

1

6

I am trying to add a UIView with "Done" button as an input accessory view to the text field.

        let view = UIView()
        let doneButton = UIButton(type: .Custom)
        doneButton.setTitle("Done", forState: .Normal)
        doneButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(doneButton)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[button]-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: ["button":doneButton]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[button]|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: ["button":doneButton]))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: doneButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)) // Even this does not work
        self.emailTextField.inputAccessoryView = view

But however I cannot see the views height being set nor the buttons in the View Hierarchy debugger/inspector in Xcode.

But if I add a view by setting its frame I can see the view being added. Also I tried setting height constraint forcibly to a constant 21 and it broke some other constraints which I had not added _UIKBAutolayoutHeightConstraint

"<NSLayoutConstraint:0x7fa3c962be50 UIView:0x7fa3c963bf60.height == UIButton:0x7fa3c963c0d0.height + 21>",
    "<NSLayoutConstraint:0x7fa3c95e0a90 '_UIKBAutolayoutHeightConstraint' V:[UIView:0x7fa3c963bf60(0)]>"

Any one faced this issue before ?

Widescreen answered 10/4, 2016 at 16:45 Comment(2)
Why do you want to do button and view setup in the code? You could do it in the interface builder.Maternity
@Maternity I am just doing everything in code instead of building views. So even this is added programmatically.Widescreen
C
-2

Swift 3+

  1. You need to specify size of toolbar view in first line.
  2. Don't use 'view' as variable in viewcontroller class since it creates confusion vs self.view

    override func viewDidLoad() {
        super.viewDidLoad()
        let toolBar = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 50))
        toolBar.backgroundColor = .gray
        let doneButton = UIButton(type: .custom)
        doneButton.setTitle("Done", for: .normal)
        doneButton.translatesAutoresizingMaskIntoConstraints = false
        toolBar.addSubview(doneButton)
        toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[button]-|", 
                                                              options: .directionLeadingToTrailing, 
                                                              metrics: nil, 
                                                              views: ["button":doneButton]))
        toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[button]|", 
                                                              options: .directionLeadingToTrailing, 
                                                              metrics: nil, 
                                                              views: ["button":doneButton]))
        self.emailTextField.inputAccessoryView = toolBar 
    }
    
Catie answered 2/10, 2017 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.