ldenoue
ldenoue

Reputation: 31

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. What's a good way to add a button on the right side of the UITextField?

Upvotes: 2

Views: 7028

Answers (3)

kervich
kervich

Reputation: 487

You might want to use the rightView property of the UITextField. Basically, there are two properties (this one and leftView accordingly) that allow you to place custom views/controls inside the UITextField. You can control whether those views are shown or not by means of rightViewMode/leftViewMode properties:

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. 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. 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:

@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.

Upvotes: 4

Yonathan Jm
Yonathan Jm

Reputation: 442

first, implement your UIButton on the header file

.h

@properties (nonatomic, strong) UIButton *headerButton;

then at implementation file (.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

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

Upvotes: 0

Nick Bull
Nick Bull

Reputation: 4276

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.

As long as you add it to your view after you add the UITextField, then it will be visible above it.

Upvotes: 0

Related Questions