2016-09-01 74 views
0

我的Webview帶有下面的工具欄。出現鍵盤時,我想在鍵盤上顯示工具欄。如何實現這一目標?如何在UIWebView上顯示鍵盤上方的工具欄

+0

取決於您如何定義佈局。如果你使用的是自動佈局,那麼我想你有一個約束將工具欄的底部粘貼到主視圖的底部。然後,您可以在鍵盤顯示時更改此約束的常量。如果您使用純粹的框架,則可以在鍵盤顯示時更改工具欄的y原點。 要檢測鍵盤顯示/關閉的時間,請參閱:http://stackoverflow.com/a/4374515/3844377 – Randy

+0

我正在使用Autolayout。你能指導如何在鍵盤出現時改變約束嗎? – Signcodeindie

+1

[如何在鍵盤上添加ToolBar?]可能的重複?(http://stackoverflow.com/questions/23904848/how-to-add-toolbar-above-keyboard) –

回答

1

下面是一個基本的架構來實現你想要的:

#import "ViewController.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIWebView *webView; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(keyboardDidShowNotification:) 
               name: UIKeyboardDidShowNotification 
               object: nil]; 

    [[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(keyboardDidHideNotification:) 
               name: UIKeyboardDidHideNotification 
               object:nil]; 
} 


-(void) keyboardDidShowNotification: (NSNotification *) notification { 

    NSDictionary *dict = [notification userInfo]; 
    CGRect keyboardFrame = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

    self.view.frame = CGRectMake(0,0, 
           self.view.frame.size.width, 
           keyboardFrame.origin.y); 
} 


-(void) keyboardDidHideNotification: (NSNotification *) notification { 
    self.view.frame = [UIApplication sharedApplication].keyWindow.frame; 
} 

-(void) dealloc{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

@end 

希望它能幫助。

0

我沒有代碼示例,但用文字我可以給出一個提示如何做到這一點。 您需要爲工具欄和屏幕底部(或放置的任何位置)之間的高度創建NSLayoutConstraint。 然後在出現鍵盤後,您需要計算鍵盤的高度並調整工具欄高度的佈局約束,以使其位於鍵盤上方。然後在鍵盤關閉時將其改回。

希望它有幫助!