如何在横向模式下将完成按钮添加到键盘?

时间:2011-11-14 11:01:54

标签: iphone uiscrollview uitextfield uikeyboard

我有一个应用程序,其中有一个文本字段。当我点击它时屏幕滚动并显示键盘。我使用这段代码:

- (void) viewWillAppear:(BOOL)animated {

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    [self willAnimateRotationToInterfaceOrientation:orientation duration:0];

    [super viewWillAppear:animated];
    NSLog(@"Registering for keyboard events");

    // Register for the events
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector (keyboardDidShow:)  name: UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];

    // Setup content size
    scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT);

    //Initially the keyboard is hidden
    keyboardVisible = NO;
}

- (void)keyboardWillShow:(NSNotification *)note {   
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for (keyboard in tempWindow.subviews) {
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            if (numberPadShowing) {
                [self addButtonToKeyboard];
                return;                 
                break;
            } else {
                for (UIView *v in [keyboard subviews]){
                    if ([v tag]==123)
                        [v removeFromSuperview];
                }
            }
    }
}

-(void) keyboardDidShow: (NSNotification *)notif {
    NSLog(@"Keyboard is visible");
    // If keyboard is visible, return
    if (keyboardVisible)
    {
        NSLog(@"Keyboard is already visible. Ignore notification.");
        return;
    }

// if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [self addButtonToKeyboard];
    }    


    // Get the size of the keyboard.
    NSDictionary* info = [notif userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Save the current location so we can restore
    // when keyboard is dismissed
    offset = scrollView.contentOffset;

    // Resize the scroll view to make room for the keyboard
    CGRect viewFrame = scrollView.frame;
    viewFrame.size.height -= keyboardSize.height;
    scrollView.frame = viewFrame;
    UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
    {
        CGRect textFieldRect = [myActiveTextField frame];
        textFieldRect.origin.y += 20;
        [scrollView scrollRectToVisible:textFieldRect animated:YES];
    } else {
        CGRect textFieldRect = [myActiveTextField frame];
        textFieldRect.origin.y += 80;
        [scrollView scrollRectToVisible:textFieldRect animated:YES];

    }
    NSLog(@"ao fim");
    // Keyboard is now visible
    keyboardVisible = YES;
}

 -(void) keyboardDidHide: (NSNotification *)notif {
    // Is the keyboard already shown
    if (!keyboardVisible) {
        NSLog(@"Keyboard is already hidden. Ignore notification.");
        return;
    }

    // Reset the frame scroll view to its original value
    scrollView.frame = CGRectMake(0, 44, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);

    // Reset the scrollview to previous location
    scrollView.contentOffset = offset;

    // Keyboard is no longer visible
    keyboardVisible = NO;
}


- (void)addButtonToKeyboard {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
    {   
        doneButton.frame = CGRectMake(0, 260, 160, 40);
    } else {
        doneButton.frame = CGRectMake(0, 163, 106, 53);
    }
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButton addTarget:self action:@selector(doneButton) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }
}

现在问题是它在纵向模式下工作正常,但在横向模式下,当我单击文本字段时,键盘将出现,并且视图将滚动太多,以至于正在编辑的文本字段在屏幕上不再可见。同样在数字键盘上(如在Phone.app中找到的那个),在横向模式下没有自定义添加按钮,但在纵向模式下有一个。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我在横向模式下使用此代码添加完成按钮

- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{   
    doneButton.frame = CGRectMake(0, 123, 160, 39);

}
else{
    doneButton.frame = CGRectMake(0, 163, 106, 53);
}
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {        
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:@selector(doneButton) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
    keyboard = [tempWindow.subviews objectAtIndex:i];
    // keyboard found, add the button
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            [keyboard addSubview:doneButton];
    } else {
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
}
  }

此方法适用于数字键盘的两种模式。