2013-10-16 15 views
6

Sự cố tôi gặp phải là khi người dùng chạm vào thanh tìm kiếm, bàn phím sẽ xuất hiện tốt. Sau đó, khi phương pháp presentQR (xem bên dưới) được gọi và sau đó được loại bỏ và sau đó thanh tìm kiếm được chạm vào bàn phím xuất hiện như được hiển thị trong ảnh chụp màn hình mà nó được bù đắp. Đây là iOS7; không chắc chắn nếu điều đó quan trọng mặc dù.Cách sửa bàn phím bù đắp khi UISearchbar bị chạm

enter image description here

tôi thêm một UISearchBar sử dụng đoạn mã sau:

// search bar 
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 200.0, 44.0)]; 
searchBarView.autoresizingMask = 0; 

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 190, 40)]; 
searchBar.backgroundImage = [[UIImage alloc] init]; 
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
self.navigationItem.titleView = searchBar; 
searchBar.delegate = self; 


[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIColor whiteColor],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal]; 

UITextField *textfield = [[UITextField alloc] init]; 
searchBar.placeholder = @"City Search"; 

// hide magnifying glass 
textfield = nil; 

for (UIView* subView in searchBar.subviews) { 
    if ([subView isKindOfClass:[UITextField class]]) { 
     textfield = (UITextField*)subView; 
     [(UITextField*)subView setTextColor:[UIColor whiteColor]]; 
     [(UITextField*)subView setFont:[UIFont fontWithName:@"DIN-Regular" size:18]]; 

     textfield.leftViewMode = UITextFieldViewModeNever; 
     textfield.rightViewMode = UITextFieldViewModeAlways; 

     break; 
    } 
} 


UIButton *cancelButton; 
for (id button in searchBar.subviews) 
{ 
    if ([button isKindOfClass:[UIButton class]]) 
    { 
     cancelButton=(UIButton*)button; 
     break; 
    } 
} 

[searchBar addSubview:textfield]; 
[searchBarView addSubview:searchBar]; 

self.navigationItem.titleView = searchBarView; 




    -(void)presentQR { 

    QRReaderViewController *qr = [[QRReaderViewController alloc]initWithNibName:@"QRReaderViewController" bundle:nil]; 

    UIButton *removeQRBtn=[UIButton buttonWithType:UIButtonTypeCustom]; 
    removeQRBtn.frame=CGRectMake(260, 10, 60, 40); 
    [removeQRBtn addTarget:self action:@selector(removeQR) forControlEvents:UIControlEventTouchDown]; 
    [removeQRBtn setImage:[UIImage imageNamed:@"qrcode.png"] forState:0]; 

    [qr.view addSubview:removeQRBtn]; 
    qr.view.backgroundColor = customColorGrey; 
    qr.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 
// [self presentModalViewController:qr animated:YES]; 
    [self presentViewController:qr animated:YES completion:nil]; 

} 

-(void)removeQR { 

// [self dismissModalViewControllerAnimated:YES]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
+0

bạn có thể tải lên nhiều ảnh chụp màn hình? trước khi làm presentQR – Mojtaba

Trả lời

4

1st Hãy chắc chắn rằng trong QRReaderViewController có gì giống như Tái định hình cửa sổ hoặc như là. Có thể có một phần tử xung đột với bàn phím ngăn chặn UIWindow được hiển thị chính xác. (ví dụ: thay đổi hướng không cân bằng.)

Thứ hai Nếu bạn không tìm thấy bất kỳ thứ gì ở đó, bạn có thể thử truy cập chế độ xem bàn phím bằng mã sau và thử đặt lại khung. Bạn có thể đặt mã này trong phương pháp keyboardWillShow (cần phải đăng ký thông báo)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillShowNotification

PS. Tôi sẽ đề nghị chỉ cần đăng nhập khung bàn phím để bạn biết khi nào nó được thay đổi.

Tái định hình Bàn phím: Tìm thấy ở đây: https://stackoverflow.com/a/10957843/1017340

//The UIWindow that contains the keyboard view - It some situations it will be better to actually 
//You need to iterate through each window to figure out where the keyboard is, but In my applications case 
//I know that the second window has the keyboard so I just reference it directly 
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 


//Because we cant get access to the UIPeripheral throughout the SDK we will just use UIView. 
//UIPeripheral is a subclass of UIView anyways 
UIView* keyboard; 

//Iterate though each view inside of the selected Window 
for(int i = 0; i < [tempWindow.subviews count]; i++) 
{ 
    //Get a reference of the current view 
    keyboard = [tempWindow.subviews objectAtIndex:i]; 

    //Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard" 
    if([[keyboard description] hasPrefix:@"<UIPeripheral"] == YES) 
    { 
     NSLog(@"Keyboard Frame: %@",NSStringFromCGRect(keyboard.frame)); 
     //HERE : try reframing the keyboard 
     //[keyboard setFrame:CGRectMake(...)]; /*NOT TESTED NOT RECOMMENDED*/ 
    } 
} 
Các vấn đề liên quan