微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法同时满足 InputAccessoryView 的约束

如何解决无法同时满足 InputAccessoryView 的约束

我有一个带有单个按钮的 InputAccessoryView。但是,我得到以下输出

enter image description here

我已经在我的 InputAccessoryView 中设置了我的 ViewController,如下所示;

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 打破了在我的视图框架中设置的 50 的高度限制。有什么原因吗??

自定义配件输入视图类

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。