If you come across a requirement where you want to add a custom view just above the UIKeyBoard, then you can use below code to do that.
If you want to show custom view on tapping inside any UITextField or UITextView, Write below line of code in their delegate methods. I am doing it for UITextView.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[textView setInputAccessoryView:[self getTextFieldAccessoryView]];
return YES;
}
Here is the method to create custom view:-
-(UIView *)getTextFieldAccessoryView{
//create your own custom view here...
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
[cancel setFrame:CGRectMake(0, self.view.bounds.size.height+1, 320.0, 40.0)];
[cancel setBackgroundImage:[UIImage imageNamed:@"yellow-button-bg"] forState:UIControlStateNormal];
[cancel setTitle:@"Cancel" forState:UIControlStateNormal];
[cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
return cancel;
}
-(void)cancelTapped{ //do whatever you want...
[self.view endEditing:YES];
}
0 Comment(s)