我正在尝试向Swift中的UITextField添加叠加视图(添加发送按钮等)。 UITextField的Apple API参考(以及Google上的其他任何地方)仅显示如何在Objective-C中执行此操作(如下所示)。任何人都可以告诉我如何在Swift中执行此操作,或指向我更新的API参考?提前谢谢!
UIButton* overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"bookmark"] forState:UIControlStateNormal];
[overlayButton addTarget:self action:@selector(displayBookmarks:)
forControlEvents:UIControlEventTouchUpInside];
overlayButton = CGRectMake(0, 0, 28, 28);
// Assign the overlay button to a stored text field
self.textField.leftView = overlayButton;
self.textField.leftViewMode = UITextFieldViewModeAlways;
答案 0 :(得分:0)
Swift翻译的代码将是
let overlayButton = UIButton(type: .custom)
overlayButton.setImage(UIImage(named: ""), for: .normal)
overlayButton.addTarget(self, action: #selector(displayBookmarks(_:)), for: .touchUpInside)
overlayButton = CGRectMake(0, 0, 28, 28);
// Assign the overlay button to a stored text field
self.textField.leftView = overlayButton;
self.textField.leftViewMode = UITextFieldViewModeAlways;
答案 1 :(得分:-1)
试试这个:
self.textField.addSubview(overlayButton)