swift3 - 如何在leftBarButtonItems中将文本字段添加为自定义视图,此处文本字段需要能够输入
问题描述
如何添加一个新视图,该视图中包含文本字段和按钮,以及如何在 leftbarbuttonitmes 中添加此视图,并且文本字段需要能够输入。
现在我已成功将此视图添加到 leftbarbuttonitems 但我的文本字段无法输入。如果我们添加leftbarbuttonitems,有什么方法可以输入。
解决方案
在这里:只需将以下代码粘贴到您的 viewDidLoad 方法中。
let viewLeftButton: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 160.0, height: 32.0))
let textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 100.0, height: viewLeftButton.frame.size.height))
textField.borderStyle = .roundedRect
textField.font = UIFont.systemFont(ofSize: 14.0)
textField.placeholder = "Type here..."
let button: UIButton = UIButton(frame: CGRect(x: textField.frame.size.width + 8, y: 0, width: 44.0, height: viewLeftButton.frame.size.height))
button.setTitle("Click", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
button.setTitleColor(.blue, for: .normal)
viewLeftButton.addSubview(textField)
viewLeftButton.addSubview(button)
self.navigationItem.hidesBackButton = true
let leftBarButton = UIBarButtonItem(customView: viewLeftButton)
self.navigationItem.leftBarButtonItem = leftBarButton
推荐阅读
- javascript - Angular - 拒绝应用 Cropperjs 的样式和脚本
- azure-devops - 我得到 ##[error]No code coverage results were found to publish。尝试在 Azure DevOps 中发布我的 create-react-app 代码覆盖率时
- r - 使用离散化后分类变量级别的标签
- javascript - 在不触发 dispatch() 的情况下更新 state props - React Redux
- python - 将一年中的某一天转换为日期时间
- angular - TypeScript - 变量分配上的 SetTimeOut 错误
- c# - 检查数组是否包含相同的元素(顺序不重要)c#
- android - 如何在没有查询的情况下在 URL 中传递数据
- reactjs - 在 React 中处理 Child 中的父函数调用
- stm32f4 - STM32F4 上的通用选项字节