简体   繁体   English

如何在键盘顶部添加视图?

[英]How to add a view on top of keyboard?

I have added a view with buttons on top of keyboard using setInputAccessoryView . 我使用setInputAccessoryView在键盘顶部添加了一个带按钮的视图。 Now i want functionality like when i click button from the view i want to display a pickerView on top of keyboard. 现在我想要的功能就像当我从视图中单击按钮时我想在键盘上显示一个pickerView Means like adding pickerView as subview of keyboard. pickerView添加为键盘子视图的方法。

How can i do this? 我怎样才能做到这一点?

Make a view you want to at the top of the keyboard. 在键盘顶部创建您想要的视图。 You can do this from xib or manually, but I think xib will be a better option. 您可以从xib或手动执行此操作,但我认为xib将是更好的选择。

Then make the reference to this view by doing this 然后通过执行此操作来引用此视图

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
   textField.inputAccessoryView = YOURCustomView;
   return YES;
}

Whenever you want to hide this or remove this just put this 无论何时你想隐藏这个或删除它只是把它

textField.inputAccessoryView = nil;
self.pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 194, 320, 224)];
    self.pickerContainerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.pickerContainerView];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    [pickerToolbar sizeToFit];

    self.lblPickerViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 230, 30)];
        self.lblPickerViewTitle.backgroundColor = [UIColor clearColor];
    [self.lblPickerViewTitle  setFont: [UIFont fontWithName:@"Arial-BoldMT" size:17]];
        self.lblPickerViewTitle.textAlignment = UITextAlignmentLeft;
        self.lblPickerViewTitle.textColor =[UIColor whiteColor];
    [pickerToolbar addSubview:self.lblPickerViewTitle];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(closePickerView:)];
    [barItems addObject:btnCancel];
    [btnCancel release];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"ShowPicker" style:UIBarButtonItemStyleBordered target:self action:@selector(donePickerView:)];
    [barItems addObject:btnDone];
    [btnDone release];

    [pickerToolbar setItems:barItems animated:YES];
    [barItems release];
    [self.pickerContainerView addSubview:pickerToolbar];

And in ShowPicker Method , write code for display UIPicker ShowPicker方法中,编写显示UIPicker的代码

You need to create another UIWindow first: 您需要先创建另一个UIWindow:

UIWindow *newWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = UIWindowLevelStatusBar; // this will make to place your WINDOW above the keyboard
[newWindow makeKeyAndVisible]; // show the new window

// [newWindow addSubview:yourView];

like add button on keyboard you can also add view like bellow... 喜欢在键盘上添加按钮,你也可以添加像下面的视图...

here you find window of keyboard and then set frame of button and also yourView and then add to keyboard 在这里你找到键盘的窗口,然后设置按钮框架和你的视图,然后添加到键盘

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:nil];
    return YES;
}
- (void)keyboardDidShow:(NSNotification *)note {
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBtn.frame = CGRectMake(0,-25,320,25);
    returnBtn.adjustsImageWhenHighlighted = NO;
    returnBtn.backgroundColor=[UIColor darkGrayColor];
    returnBtn.titleLabel.textColor=[UIColor whiteColor];
    [returnBtn setBackgroundImage:[UIImage imageNamed:@"keyBtn.png"] forState:UIControlStateNormal];

    [returnBtn addTarget:self action:@selector(keyboardBtn:) 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([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            //  if (txtTag==5) {
            [keyboard addSubview:returnBtn];
    }
}
-(IBAction)keyboardBtn:(id)sender{
    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([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            //  if (txtTag==5) {
            [keyboard addSubview:yourView];// here add your view with frame
    }
}

i hope this help you... 我希望这能帮到你......

:) :)

From Jonas Schnelli's answer 来自Jonas Schnelli的回答

UIWindow *newWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = CGFLOAT_MAX; // this will make to place your WINDOW above the keyboard

[newWindow addSubview:yourView];

Just change UIWindowLevelStatusBar to CGFLOAT_MAX will be ok. 只需将UIWindowLevelStatusBar更改为CGFLOAT_MAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM