2012-10-04 63 views
7

Tôi đã thêm chế độ xem có các nút ở trên cùng của bàn phím bằng cách sử dụng setInputAccessoryView. Bây giờ tôi muốn chức năng như khi tôi nhấp vào nút từ chế độ xem, tôi muốn hiển thị pickerView ở đầu bàn phím. Có nghĩa là thêm pickerView làm chế độ xem phụ của bàn phím.Cách thêm chế độ xem trên đầu bàn phím?

Tôi làm cách nào để thực hiện việc này?

+0

bạn có ví dụ về ứng dụng thực hiện việc này không? – Jessedc

Trả lời

1

Bạn cần phải tạo UIWindow khác đầu tiên:

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]; 
+0

'UIWindow * newWindow = [[Giao diện UIWindow] initWithFrame: CGRectMake (0.0, 250, 320, 200)]; newWindow.windowLevel = UIWindowLevelStatusBar; [newWindow makeKeyAndVisible]; [newWindow addSubview: self.myView]; 'Nhưng nó không hoạt động. – user1244311

+0

mô tả: nó không hoạt động .. :) –

1

thích thêm nút trên bàn phím của bạn cũng có thể thêm xem như dưới đây ...

đây bạn tìm thấy cửa sổ của bàn phím và sau đó thiết lập khung nút và cũng yourView và sau đó thêm vào bàn phím

- (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 
    } 
} 

tôi hy vọng điều này giúp bạn ...

:)

2
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]; 

Và trong ShowPicker Method, viết mã để hiển thị UIPicker

+0

Vấn đề của tôi là tôi có thể có thể thêm pickerView như subView của quan điểm của tôi nhưng bàn phím là ẩn đó. vì vậy những gì tôi muốn khi tôi nhấp vào nút tôi muốn hiển thị giao diện bộ chọn ở đầu bàn phím. – user1244311

+0

vấn đề ur là bàn phím không hiển thị và u wn để hiển thị pickerview trên ur keyboed ??? m tôi phải không ?? –

+0

Tôi đã thêm chế độ xem ở đầu bàn phím. chế độ xem đó có các nút. khi tôi nhấp vào nút, tôi muốn hiển thị chế độ xem bộ chọn thay cho bàn phím. nhưng vấn đề là chế độ xem được thêm vào bàn phím. vì vậy nếu bạn ẩn bàn phím, các nút bấm cũng sẽ bị ẩn. – user1244311

19

Thực hiện một quan điểm mà bạn muốn ở phía trên cùng của bàn phím. Bạn có thể làm điều này từ xib hoặc bằng tay, nhưng tôi nghĩ xib sẽ là một lựa chọn tốt hơn.

Sau đó thực hiện tham chiếu đến quan điểm này bằng cách làm này

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

Bất cứ khi nào bạn muốn ẩn này hoặc loại bỏ điều này chỉ cần đặt này

textField.inputAccessoryView = nil; 
0

Từ câu trả lời của 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]; 

Chỉ cần thay đổi UIWindowLevelStatusBar thành CGFLOAT_MAX sẽ ổn.

Các vấn đề liên quan