【发布时间】:2014-11-18 15:16:07
【问题描述】:
UITextField 可以单独使用代码来完成此 UI,还是我需要为此使用图像或使用其他组件?比如按钮、标签。
【问题讨论】:
标签: ios uibutton uitextfield
UITextField 可以单独使用代码来完成此 UI,还是我需要为此使用图像或使用其他组件?比如按钮、标签。
【问题讨论】:
标签: ios uibutton uitextfield
你可以通过使用UITextField、UIButton和UITableView和一些动画来实现这一点,无论如何一旦看看这个例子ios dropdown
【讨论】:
UITextField 不提供这种功能。您需要使用UIImageView、UIButton 和UITextField,在UIImageView 中设置包含您的文本字段背景图像的图像,禁用文本字段的 UserInteraction 属性并将其保留在 imageView 上,使用没有标题的自定义按钮并执行您想要的带有按钮的操作。
【讨论】:
试试这个。
UIImageView *imgViewForDropDown = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
imgViewForDropDown.image = [UIImage imageNamed:@"dropDown.png"];
yourTextField.rightView = imgViewForDropDown;
yourTextField.rightViewMode = UITextFieldViewModeAlways;
对于 Swift 代码:
let imgViewForDropDown = UIImageView()
imgViewForDropDown.frame = CGRect(x: 0, y: 0, width: 30, height: 48)
imgViewForDropDown.image = UIImage(named: "ic_keyboard_arrow_down_white")
yourTextField.rightView = imgViewForDropDown
yourTextField.rightViewMode = .always
【讨论】:
这行得通:
fileprivate func addBtnToTextField() {
let dropDownBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
dropDownBtn.setBackgroundImage(UIImage(named: "dropDownTringle"), for: UIControlState.normal)
startDateTxt.rightViewMode = UITextFieldViewMode.always
startDateTxt.rightView = dropDownBtn
}
【讨论】: