0


In my iPhone application I have a UIScrollView with several UITextFields.
Using BSKeyboardControls I have added Prev/Next/Done buttons to move between the fields. However, the focus on the selected field is not working, meaning that the text field is actually still under the keyboard although selected.
becomeFirstResponder is activated but just don't set the focus.
Any ideas what might be wrong?
Thanks

In the H file

#import "BSKeyboardControls.h"
...
@interface AddClientViewController : BaseViewController<UIAlertViewDelegate, UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate>
...
@property (strong, nonatomic) IBOutlet UITextField *firstName;
@property (strong, nonatomic) IBOutlet UITextField *lastName;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *mobile;
@property (strong, nonatomic) IBOutlet UITextField *birthday;
@property (strong, nonatomic) IBOutlet UITextField *anniversary;
@property (strong, nonatomic) IBOutlet UITextField *street;
@property (strong, nonatomic) IBOutlet UITextField *city;
@property (strong, nonatomic) IBOutlet UITextField *state;
@property (strong, nonatomic) IBOutlet UITextField *zip;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, strong) BSKeyboardControls *keyboardControls;
....

In M file

- (void)viewDidLoad
{
...
    NSArray *fields = @[ self.firstName, self.lastName,
    self.email, self.mobile,
    self.birthday, self.anniversary,
    self.street, self.city, self.state, self.zip];

    [self setKeyboardControls:[[BSKeyboardControls alloc] initWithFields:fields]];
    [self.keyboardControls setDelegate:self];
}

- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls
{
    [keyboardControls.activeField resignFirstResponder];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls directionPressed:(BSKeyboardControlsDirection)direction
{
    UIView *view = keyboardControls.activeField.superview.superview;
    [self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls selectedField:(UIView *)field inDirection:(BSKeyboardControlsDirection)direction
{
    UIView *view = keyboardControls.activeField.superview.superview;
    [self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.keyboardControls setActiveField:textField];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.keyboardControls setActiveField:textView];
}

The setActiveField in BSKeyboardControls

- (void)setActiveField:(id)activeField
{
    if (activeField != _activeField)
    {
        if ([self.fields containsObject:activeField])
        {
            _activeField = activeField;

            if (![activeField isFirstResponder])
            {
                [activeField becomeFirstResponder];
            }

            [self updateSegmentedControlEnabledStates];
        }
    }
}
urir
  • 1,960
  • 3
  • 23
  • 40

1 Answers1

4

Since you're using a UIScrollView with UITextFields, you can use the scrollRectToVisible method for the UIScrollview, in a method that'd be roughly like this:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [_myScrollView scrollRectToVisible:textField.frame animated:YES];
}

To do this, you'll need to make sure that the UIViewController is the UITextFieldDelegate of each of the textfields in the scrollview. You could do this in the viewDidLoad method of your UIViewController:

[textField1 setDelegate:self];
[textField2 setDelegate:self];

...and so on

bryanjclark
  • 6,247
  • 2
  • 35
  • 68
  • Hi, done all of the above. Still not working. I feel something is wrong actually with the way I did the scroll view, but not sure what exactly... – urir Feb 05 '13 at 07:32
  • In that case, I'd throw in some breakpoints into that code and figure out what methods are or aren't being called as you tap the prev/next/done buttons. – bryanjclark Feb 05 '13 at 07:38
  • All methods are called :) I am over that. What I actually found out is that it works, just works bad (or I am using it wrong). For example if I choose first field and then scroll down to the bottom and press next - the next field is actually scrolled to! :) also for the next fields... BUT, the 3rd field and so on are scrolled and presented on the screen while being UNDER the keyboard :( so I probably need to scroll it better... How? ;) – urir Feb 05 '13 at 07:43
  • sounds like your UIScrollView is covered up by the keyboard. Make sure to set the scrollview's frame to only include the space above the keyboard, and then have its contentSize include the textfields. You can find out more about resizing a UIScrollView to compensate for a keyboard in this question: http://stackoverflow.com/questions/7169702/how-to-resize-uitextview-on-ios-when-a-keyboard-appears – bryanjclark Feb 05 '13 at 07:46
  • Thanks Bryan, I took the stuff from the link, well it moves now :) jumps too much but I guess I will need to try debug it... – urir Feb 05 '13 at 07:57
  • I guess keyboard gets messed up for the field that is used the picker, need find something instead of UIKeyboardWillShowNotification for the picker... – urir Feb 05 '13 at 08:01
  • Debug result - actually it gets messed when appears the picker view – urir Feb 05 '13 at 08:10
  • 1
    finally I went for this solution: [scrollView setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES]; http://stackoverflow.com/questions/7193787/keyboard-scroll-on-active-text-field-scrolling-to-out-of-view – urir Feb 05 '13 at 08:35
  • please give me demo link of bskeyboardcontrols in scrollview. – Darshan Kunjadiya Jul 02 '13 at 08:09
  • 1
    This fixed an issue I had with BSKeyboardControls in a UITableview, thanks! – Leon Jul 10 '15 at 14:51