• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 在 UITextField 中添加按钮

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:56:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在开发适用于 iOS 的浏览器应用程序,并希望在位置栏的文本字段内添加一个“阅读器”按钮,就像 Mobile Safari 在检测到该页面是一篇文章时所做的那样。 在 UITextField 右侧添加按钮有什么好方法?



Best Answer-推荐答案


您可能想要使用 UITextField 的 rightView 属性。基本上,有两个属性(这个和相应的 leftView)允许您在 UITextField 中放置自定义 View /控件。您可以通过 rightViewMode/leftViewMode 属性控制是否显示这些 View :

UIButton *myButton = [UIButton new];
// configure your button here
myTextField.rightView = myButton;
myTextField.rightViewMode = UITextFieldViewModeUnlessEditing; // check the docs for other possible values

另外,还有一个名为 rightViewRectForBounds: 的方法,它返回一个 CGRect ,您的自定义 View /控件将在其中运行。你不应该直接在那里传递你的按钮的尺寸,相反,如果你想放置的东西肯定比这个方法返回的 rect 大(当你在其中放置 View 时 UITextField 代表你调用它),你的 View 将被缩放以适应这个矩形。在这种情况下,您可能想要覆盖它(创建您的类别或 UITextField 的子类。假设您选择一个类别,您的 UITextField+MyButton 应该如下所示:

@interface UITextField(MyButton)
- (CGRect)rightViewRectForBoundsCGRect)bounds;
@end

@implementation UITextField(MyButton)

- (CGRect)rightViewRectForBoundsCGRect)bounds
{
    // return the CGRect that would fit your view/button. The buttons param that is passed here is the size of the view that is being added to the textField.
}

// possibly some other methods that you override

@end

希望这会有所帮助。如另一个答案所述,请先查看文档,看看有哪些方法可以帮助您。

关于iphone - 在 UITextField 中添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8927505/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap