1

I have an InputAccessoryView with a single button in. However, I am getting the following output.

enter image description here

I have set up my InputAccessoryView in my ViewController as below;

lazy var customInputView: ButtonInputView = {
    let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
    let view = ButtonInputView(frame: frame)
    view.doneButton.addTarget(self, action: #selector(signIn), for: .touchUpInside)
    return view
}()

override var inputAccessoryView: UIView? {
    return customInputView
}

Xcode is breaking the height constraint of 50 set in the frame of my view. Any reason why??

CUSTOM ACCESSORY INPUT VIEW CLASS

class ButtonInputView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var doneButton: UIButton = {
        let view = UIButton()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = Color.accent
        view.setTitle("Done", for: .normal)
        view.titleLabel?.font = Font.mainButton
        view.layer.cornerRadius = 19
        return view
    }()

    let separator: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .opaqueSeparator
        return view
    }()

    private func setUpView() {
        backgroundColor = Color.background
        addSubview(doneButton)
        addSubview(separator)
        let separatorHeight = (1 / (UIScreen.main.scale))

        if let titleLabelText = doneButton.titleLabel?.text {
            doneButton.widthAnchor.constraint(equalToConstant: titleLabelText.width(usingFont: Font.mainButton) + 32).isActive = true
        }

        NSLayoutConstraint.activate([
            separator.topAnchor.constraint(equalTo: topAnchor),
            separator.leadingAnchor.constraint(equalTo: leadingAnchor),
            separator.trailingAnchor.constraint(equalTo: trailingAnchor),
            separator.heightAnchor.constraint(equalToConstant: separatorHeight),
            doneButton.topAnchor.constraint(equalTo: separator.bottomAnchor, constant: 6),
            doneButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
            doneButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -6),
        ])
    }
    
    override var intrinsicContentSize: CGSize {
        return CGSize.zero
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        if #available(iOS 11.0, *) {
            if let window = window {
                bottomAnchor.constraint(lessThanOrEqualToSystemSpacingBelow: window.safeAreaLayoutGuide.bottomAnchor,
                                        multiplier: 1.0).isActive = true
            }
        }
    }
}
1
  • What are you trying to accomplish in your didMoveToWindow() func? Without that (or just not assigning the bottomAnchor.constraint(...) inside it), the conflict goes away. Commented Apr 27, 2021 at 21:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.