Ios 如何设置UITextField';s inputAccessoryView?

Ios 如何设置UITextField';s inputAccessoryView?,ios,uitextfield,inputaccessoryview,Ios,Uitextfield,Inputaccessoryview,为了在我的UITextField键盘上有一个按钮,我这样做: class MyTextField: UITextField { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let button = UIButton(type: .system) let buttonSize = CGSize(width: button.frame.size.w

为了在我的
UITextField
键盘上有一个按钮,我这样做:

class MyTextField: UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let button = UIButton(type: .system)
        let buttonSize = CGSize(width: button.frame.size.width, height: 50)
        button.frame = CGRect(origin: button.frame.origin, size: buttonSize)
        button.backgroundColor = .orange
        button.setTitleColor(.white, for: .normal)
        button.setTitle("Test", for: .normal)
        inputAccessoryView = button
    }

}

虽然这很好,但我想在这里定制更多按钮的来源和大小,但我找不到这样做的方法。我试过这样做:

let buttonSize = CGSize(width: button.frame.size.width - 100, height: 50)
button.frame = CGRect(origin: button.frame.origin, size: buttonSize)
但是按钮保持相同的宽度。我能做什么


感谢您的帮助。

我们的想法是忽略指定给inputAccessoryView的视图框架,因此创建一个透明颜色的视图并向其添加任何项目,您可以尝试此操作

class MyTextField: UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let button = UIButton(type: .system)
        button.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.size.width-100,height: 50)
        button.backgroundColor = .orange
        button.setTitleColor(.white, for: .normal)
        button.setTitle("Test", for: .normal)
        let rr = UIView()
        rr.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.size.width,height: 50)
        rr.backgroundColor = UIColor.clear;
        inputAccessoryView = rr
        rr.addSubview(button)
    }


}

想法是忽略指定给inputAccessoryView的视图框架,因此创建一个具有透明颜色的视图并向其添加任何项目,您可以尝试此方法

class MyTextField: UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let button = UIButton(type: .system)
        button.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.size.width-100,height: 50)
        button.backgroundColor = .orange
        button.setTitleColor(.white, for: .normal)
        button.setTitle("Test", for: .normal)
        let rr = UIView()
        rr.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.size.width,height: 50)
        rr.backgroundColor = UIColor.clear;
        inputAccessoryView = rr
        rr.addSubview(button)
    }


}

好的解决方案。谢谢很好的解决方案。谢谢