I need help to try to configure the keyboard in the UIViewController: (a) do not hover over both fields of the UIText, so the scroller must be located correctly; and (b) when the user touches the background, the keyboard disappears.
I have not tried (b) yet, but I am trying (a), and the code that I received from a Google search did not give me the desired effect. In fact, this makes my text fields disappear when I touch one of them. I am sure my implementation of activeField is also incorrect. I got this example from the Apple Development Keyboard section.
The code I have is below:
@interface FirstViewController : UIViewController {
IBOutlet UITextField *emailAddress;
IBOutlet UITextField *password;
IBOutlet UIButton *loginButton;
IBOutlet UIScrollView *scroller;
BOOL keyboardShown;
UITextField *activeField;
ASIHTTPRequest *requestRequiringAuthentication;
ASINetworkQueue *networkQueue;
}
- (IBAction) LoginUser:(id)sender;
@property (nonatomic,retain) IBOutlet UITextField *emailAddress;
@property (nonatomic,retain) IBOutlet UITextField *password;
@property (nonatomic, retain) IBOutlet UIScrollView *scroller;
@property (nonatomic, retain) UITextField *activeField;
@property (retain) ASINetworkQueue *networkQueue;
@property (retain) ASIHTTPRequest *requestRequiringAuthentication;
@end
@implementation FirstViewController
@synthesize requestRequiringAuthentication;
@synthesize networkQueue;
@synthesize password;
@synthesize emailAddress;
@synthesize scroller;
@synthesize activeField;
#pragma mark -
#pragma mark LifeCycle
- (void)awakeFromNib
{
NSLog(@"awaking from nib");
[self setNetworkQueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
- (void)viewDidLoad {
NSLog(@"viewdidload");
[super viewDidLoad];
}
- (void)viewDidUnload {
}
- (void)viewWillAppear:(BOOL)animated {
keyboardShown = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewWillDisappear:animated];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if (keyboardShown)
return;
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
CGRect viewFrame = [scroller frame];
viewFrame.size.height -= keyboardSize.height;
scroller.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
[scroller scrollRectToVisible:textFieldRect animated:YES];
keyboardShown = YES;
}
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
CGRect viewFrame = [scroller frame];
viewFrame.size.height += keyboardSize.height;
scroller.frame = viewFrame;
keyboardShown = NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
source
share