2013-10-16 8 views
6

Das Problem, das ich habe, ist, dass, wenn ein Benutzer in die Suchleiste berührt die Tastatur gut aussieht. Dann, wenn die presentQR Methode (siehe unten) aufgerufen und dann entlassen und dann die Suchleiste berührt wird, erscheint die Tastatur wie auf dem Screenshot gezeigt, wo es versetzt ist. Das ist iOS7; nicht sicher, ob das aber zählt.Wie man Offset-Tastatur zu reparieren, wenn UISearchbar berührt wird

enter image description here

Ich füge einen UISearchBar mit dem folgenden Code:

// 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

könnten Sie weitere Screenshots hochladen? bevor du presentQR machst – Mojtaba

Antwort

4

1. Vergewissern Sie sich, dass in QRReaderViewController es nichts Fenster oder dergleichen Reframing. Möglicherweise liegt ein Element im Konflikt mit UIWindow, das verhindert, dass die Tastatur korrekt angezeigt wird. (z. B. unsymmetrische Ausrichtung ändert.)

2. Wenn Sie dort nichts gefunden haben, können Sie versuchen, mit dem folgenden Code auf die Tastaturansicht zuzugreifen, und versuchen, den Rahmen zurückzusetzen. Man könnte diesen Code in keyboardWillShow Methode setzen (müssen für die Anmeldung registrieren)

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

PS. Ich würde vorschlagen, nur den Tastaturrahmen zu protokollieren, damit Sie wissen, wann es geändert wird.

Reframing Keyboard: hier gefunden: 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*/ 
    } 
} 
Verwandte Themen