简体   繁体   English

在UITextField中添加按钮

[英]adding button inside UITextField

I'm working on a browser app for iOS and want to add a "Reader" button inside the textfield of the location bar like Mobile Safari does when it detects that the page is an article. 我正在开发适用于iOS的浏览器应用程序,并希望在位置栏的文本字段内添加一个“阅读器”按钮,就像Mobile Safari在检测到该页面为文章时一样。 What's a good way to add a button on the right side of the UITextField? 在UITextField右侧添加按钮的好方法是什么?

You might want to use the rightView property of the UITextField. 您可能要使用UITextField的rightView属性。 Basically, there are two properties (this one and leftView accordingly) that allow you to place custom views/controls inside the UITextField. 基本上,有两个属性(分别是一个和leftView ),它们允许您将自定义视图/控件放置在UITextField中。 You can control whether those views are shown or not by means of rightViewMode / leftViewMode properties: 您可以通过rightViewMode / leftViewMode属性控制是否显示这些视图:

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

Also, there's a method called rightViewRectForBounds: that returns a CGRect where your custom view/control will go. 另外,还有一个名为rightViewRectForBounds:的方法rightViewRectForBounds:该方法返回一个CGRect,您的自定义视图/控件将进入该位置。 you shouldn't pass your button's dimensions there directly, instead if you want to place something that is definitely bigger than the rect this method returns (seems like UITextField calls it on your behalf when you're placing a view inside it), your view will be scaled to fit this rect. 您不应该直接在此处传递按钮的尺寸,而是如果您想要放置绝对大于此方法返回的rect的东西(当您在其中放置视图时,似乎UITextField代表您调用它)将缩放以适合此矩形。 It that case you might want to override it (create your category or maybe subclass of UITextField. Assuming, you go for a category your UITextField+MyButton should look something like this: 在这种情况下,您可能想覆盖它(创建您的类别或UITextField的子类。假设您选择一个类别,则UITextField + MyButton应该如下所示:

@interface UITextField(MyButton)
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
@end

@implementation UITextField(MyButton)

- (CGRect)rightViewRectForBounds:(CGRect)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

hope this helps. 希望这可以帮助。 And as noted in the other answer, check the docs first to see what methods are there that can help you. 如另一个答案中所述,请首先检查文档,以查看有什么方法可以帮助您。

Just add a UIButton and position it so it overlaps the UITextField wherever you want it to be and in the size that makes it fit "inside" the UITextField . 只需添加一个UIButton并将其定位,以使其与UITextField重叠,无论您希望放置在UIButton位置,并且其大小都应使其适合UITextField “内部”。

As long as you add it to your view after you add the UITextField , then it will be visible above it. 只要在添加UITextField之后将其添加到视图中,它就会在其上方可见。

first, implement your UIButton on the header file 首先,在头文件上实现UIButton

.h 。H

@properties (nonatomic, strong) UIButton *headerButton;

then at implementation file (.m) 然后在实现文件(.m)

@synthesize headerButton;

- (void)viewDidLoad
{

    UITextField *urlField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    //your url processing code here
    //...
    //...
    //...
    [self.view addSubview:urlField];
    //declare your reader button
    readerButton = [[UIButton alloc]initWithFrame:CGRectMake(70, 10, 25, 20)];
    [readerButton addTarget:self action:@selector(readerButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    readerButton.hidden = YES;
}

finally add this to ur url processing method so that the reader button only appear when the page is readable 最后将其添加到您的url处理方法中,以使阅读器按钮仅在页面可读时出现

   //add your condition
    if( page is pdf or readable )
    {
        readerButton.hidden = NO;
    }
    else
    {
        readerButton.hidden = YES;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM