Add Custom UIButton to UICeyboard Accessory for UIWebView

I need to add a camera button to the UIWebView toolbar for keyboard accessories (one that already has the Back | Next and Finish buttons).

Is there an easy way to do this?

+1
cocoa-touch uikeyboard
source share
2 answers

I'm still looking for a better way to do this, but one solution for now would be to destroy the panel and create it like this:

 - (void)viewDidLoad { [super viewDidLoad]; UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; NSURL *aURL = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL]; [web loadRequest:aRequest]; [self.view addSubview:web]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { [self performSelector:@selector(removeBar) withObject:nil afterDelay:0]; } - (void)removeBar { UIWindow *keyboardWindow = nil; for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { if (![[testWindow class] isEqual:[UIWindow class]]) { keyboardWindow = testWindow; break; } } for (UIView *possibleFormView in [keyboardWindow subviews]) { // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView. if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) { for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) { if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) { UISegmentedControl *nav = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous",@"Next", nil]]; nav.momentary = YES; nav.frame = CGRectMake(5, 8, 120, 30); nav.segmentedControlStyle = UISegmentedControlStyleBar; nav.tintColor = [UIColor colorWithWhite:0.25 alpha:1]; [nav addTarget:self action:@selector(navigate) forControlEvents:UIControlEventValueChanged]; UIBarButtonItem *pictureBtn =[[UIBarButtonItem alloc] initWithTitle:@"Picture" style:UIBarButtonItemStyleBordered target:self action:@selector(takePic:)]; UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)]; NSArray *buttons = [NSArray arrayWithObjects:flexButton,pictureBtn,flexButton,doneButton, nil]; [(UIToolbar *)subviewWhichIsPossibleFormView addSubview:nav]; [(UIToolbar *)subviewWhichIsPossibleFormView setItems:buttons]; } } } } } 

But then you need to implement the previous, next and done function again. But this is not the hardest part.

0
source share

There is an easier way to do this.

Keep notification on keyboard in viewDidLoad

After that, you will need the following method:

 -(void)keyboardDidShow:(NSNotification*)notif { NSArray *array = [[UIApplication sharedApplication] windows]; for (UIWindow* wind in array) { for (UIView* currView in wind.subviews) { if ([[currView description] hasPrefix:@"<UIPeripheralHostView"]) { for (UIView* perView in currView.subviews) { if ([[perView description] hasPrefix:@"<UIWebFormAccessory"]) { UIBarButtonItem *doneBttn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyBoard)]; NSMutableArray *arr = [[NSMutableArray alloc] initWithArray: [(UIToolbar *) perView items]]; [arr addObject:doneBttn]; [(UIToolbar *) perView setItems:arr]; } } } } } } 

Basically, I took the original previous / next buttons from UIWebFormAccessory and added the Finish button to the end, but you can add whatever you want for this.

Some might think that this should not be implemented in -(void)keyboardDidShow: because you change the user interface after it appears on the screen, but so far I have not noticed any problems with this. (tested only in the simulator)

Hope this was helpful!

0
source share

All Articles