Iphone 在UITextField中添加按钮

Iphone 在UITextField中添加按钮,iphone,ios,sdk,uitextfield,Iphone,Ios,Sdk,Uitextfield,我正在开发一个iOS浏览器应用程序,希望在位置栏的文本字段中添加一个阅读器按钮,就像Mobile Safari在检测到页面是文章时所做的那样。 在UITextField的右侧添加按钮的好方法是什么?只需添加一个UIButton并将其放置在UITextField上,使其与UITextField重叠在您希望的任何位置,并且其大小适合UITextField内部 只要在添加UIExtField后将其添加到视图中,它就会在其上方可见。只需添加一个UIButton并将其定位,使其与UIExtField重叠,

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

只需添加一个UIButton并将其放置在UITextField上,使其与UITextField重叠在您希望的任何位置,并且其大小适合UITextField内部

只要在添加UIExtField后将其添加到视图中,它就会在其上方可见。

只需添加一个UIButton并将其定位,使其与UIExtField重叠,无论它位于何处,并且其大小适合UIExtField内部


只要在添加UITextField后将其添加到视图中,它就会在其上方可见。

首先,在头文件上实现UIButton

h

然后是实现文件.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;
}
最后,将其添加到URURL处理方法中,以便阅读器按钮仅在页面可读时出现

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

首先,在头文件上实现UIButton

h

然后是实现文件.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;
}
最后,将其添加到URURL处理方法中,以便阅读器按钮仅在页面可读时出现

   //add your condition
    if( page is pdf or readable )
    {
        readerButton.hidden = NO;
    }
    else
    {
        readerButton.hidden = YES;
    }
您可能希望使用UITextField的rightView属性。基本上,有两个属性—this one和leftView—允许您将自定义视图/控件放置在UITextField中。您可以通过rightViewMode/leftViewMode属性控制是否显示这些视图:

UIButton *myButton = [UIButton new];
// configure your button here
myTextField.rightView = myButton;
myTextField.rightViewMode = UITextFieldViewModeUnlessEditing; // check the docs for other possible values
此外,还有一个名为rightViewRectForBounds的方法:它返回自定义视图/控件所在的CGRect。您不应该直接传递按钮的尺寸,相反,如果您想放置比此方法返回的矩形更大的对象,则当您在其中放置视图时,UITextField会代表您调用它,您的视图将缩放以适应此矩形。在这种情况下,您可能需要覆盖它,创建您的类别,或者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
希望这有帮助。如另一个答案中所述,首先检查文档,看看有哪些方法可以帮助您。

您可能需要使用UITextField的rightView属性。基本上,有两个属性—this one和leftView—允许您将自定义视图/控件放置在UITextField中。您可以通过rightViewMode/leftViewMode属性控制是否显示这些视图:

UIButton *myButton = [UIButton new];
// configure your button here
myTextField.rightView = myButton;
myTextField.rightViewMode = UITextFieldViewModeUnlessEditing; // check the docs for other possible values
此外,还有一个名为rightViewRectForBounds的方法:它返回自定义视图/控件所在的CGRect。您不应该直接传递按钮的尺寸,相反,如果您想放置比此方法返回的矩形更大的对象,则当您在其中放置视图时,UITextField会代表您调用它,您的视图将缩放以适应此矩形。在这种情况下,您可能需要覆盖它,创建您的类别,或者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

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

我要做的只是创建一个全宽度的UIView。添加为子视图、背景图像、右侧的UIButton和左侧的UIExtField。我要做的只是创建一个全宽的UIView。添加为子视图、背景图像、右侧的UIButton和左侧的UIExtField。虽然我很欣赏这是个人观点,我觉得最好给出答案,作为指导提问者的指针,这样他们就可以去构建自己的知识,而不是像这样给出模板代码——否则StackOverflow开始成为复制/粘贴代码库,而不是帮助学习和开发。老实说,我对objective C也很陌生,所以很明显,我没有信心教别人事情应该如何运作。。。所以我只是想通过提供一些代码来帮助我,这些代码对我来说大部分时间都能帮助我发现新的方法来创造一些东西,虽然我很欣赏这是我个人的观点,我觉得最好给出答案,作为指导提问者的指针,这样他们就可以去构建自己的知识,而不是像这样给出模板代码——否则StackOverflow开始成为复制/粘贴代码库,而不是帮助学习和开发。老实说,我对objective C也很陌生,所以很明显,我没有信心教别人事情应该如何运作。。。所以我只是想通过给我一段代码来帮助你,对我来说,这段代码大部分时间都能帮助我发现新的方法来创建一些东西。我想,如果你只是简单地在一个文本字段上点击一个按钮,当
n打字。我会选择UITextField的rightView/leftView属性。我想,如果您只是在文本字段上单击一个按钮,您在键入时将无法看到按钮下方的文本。我会选择UITextField的rightView/leftView属性。