久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

鍵盤出現時如何使視圖控制器滾動到文本字段

How to make the view controller scroll to text field when keyboard appears(鍵盤出現時如何使視圖控制器滾動到文本字段)
本文介紹了鍵盤出現時如何使視圖控制器滾動到文本字段的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想讓我的 uiviewcontroller.xib 滾動.我的視圖控制器有 8 個文本字段.所以我的問題是當我想在第 5 個 textfield 中寫一些東西時,我的鍵盤會覆蓋文本字段.我怎樣才能擺脫這個問題,讓我的視圖控制器滾動?

由于是 iPhone 開發新手,請詳細指導.

提前致謝.

解決方案

你可以使用 是更多信息關于管理鍵盤.

這里是我的 ViewController.h 供參考

#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UITextFieldDelegate>@結尾

和 ViewController.m

#import "ViewController.h"@interface 視圖控制器 ()@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@結尾@implementation 視圖控制器- (void)viewDidLoad{[超級視圖DidLoad];//注冊鍵盤通知[[NSNotificationCenter defaultCenter] addObserver:self選擇器:@selector(keyboardWasShown:)名稱:UIKeyboardDidShowNotification 對象:無];[[NSNotificationCenter defaultCenter] addObserver:self選擇器:@selector(keyboardWillBeHidden:)名稱:UIKeyboardWillHideNotification 對象:無];}//在發送 UIKeyboardDidShowNotification 時調用.- (void)keyboardWasShown:(NSNotification*)aNotification{NSDictionary* info = [aNotification userInfo];CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;[self.scrollView setContentOffset:CGPointMake(0, kbSize.height) 動畫:YES];}//在發送 UIKeyboardWillHideNotification 時調用- (void)keyboardWillBeHidden:(NSNotification*)aNotification{[self.scrollView setContentOffset:CGPointMake(0, 0) 動畫:YES];}- (IBAction)textFieldDidBeginEditing:(UITextField *)sender {sender.delegate = 自我;}- (BOOL)textFieldShouldReturn:(UITextField *)textField {返回 [textField resignFirstResponder];}@結尾

I want to make my uiviewcontroller.xib scroll. My view controller has like 8 textfields. So my problem is when I want to write something in the 5th textfield and so on my keyboard covers the textfields. How can I get rid of this problem, and make my viewcontroller scroll?

Please guide in detail because am new to iPhone development.

Thanks in advance.

解決方案

You can use a ScrollView.

Adding the scroll view

Drag an drop a scrollView onto your view controller, the same way you would with a text field and adjust the dimensions to suit your needs (it seems like you'd want it to fill the view controller.)

Then place the text fields into the scroll view. I think it's easiest to do using the document outline on the left. Drag the text fields onto the scroll view here, like in the picture.

Making the scroll view scroll when keyboard appears

Add this code to your view controller in viewDidLoad

//register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

And add these methods to your view controller

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self.scrollView setContentOffset:CGPointMake(0, kbSize.height) animated:YES];
}
//called when the text field is being edited
- (IBAction)textFieldDidBeginEditing:(UITextField *)sender {
    sender.delegate = self;
}

The first two of these methods is called when the keyboard is shown. The second is called when you start to edit a text field.

Now go to your storyboard and attach actions of the text fields to the method that was just added. You can right click on the text field, select the appropriate action and drag it to the method.

Your should see something like this when you right click on your textfields.

Add this property to your view controller and right click drag from your scroll view to it. It allows your view controller to control the scroll view.

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

Like this:

Closing the keyboard

When the return button is pressed we want the keyboard to close.

In your view controller header make your view controller a UITextFieldDelegate Like this:

@interface ViewController : UIViewController <UITextFieldDelegate>

Add this code to your view controller in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

And add these methods to your view controller

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

The first method is called when the keyboard is closed. It returns the scroll view to its original position. The second method is called when you have finished editing a text field. It allows the keyboard to be dismissed when this happens.

More info

Here is more information on managing the keyboard.

And for reference here is my ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@end

and ViewController.m

#import "ViewController.h"

@interface ViewController () 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self.scrollView setContentOffset:CGPointMake(0, kbSize.height) animated:YES];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (IBAction)textFieldDidBeginEditing:(UITextField *)sender {
    sender.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

@end

這篇關于鍵盤出現時如何使視圖控制器滾動到文本字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
主站蜘蛛池模板: 精品中文字幕一区 | 国产精品久久视频 | 成人免费观看视频 | 9久9久9久女女女九九九一九 | 伊人久久一区二区 | 中文在线视频观看 | 精品粉嫩超白一线天av | 日韩一区二区三区在线观看 | 日韩精品1区2区3区 成人黄页在线观看 | 欧美精品在线观看 | 亚洲最大的黄色网址 | 99精品视频在线观看免费播放 | 美女视频黄的 | 粉嫩国产精品一区二区在线观看 | 亚洲国产福利视频 | 日韩欧美在 | 日韩av免费在线观看 | 狠狠色狠狠色综合日日92 | 日日天天 | 中文字幕一区二区三区乱码图片 | 91视频在线网站 | 国产视频第一页 | 国产精品123区 | 午夜免费视频 | 精品久久久久久中文字幕 | 久久国产视频一区 | 久草99 | 日韩三级电影一区二区 | 国产精品99久久久久久动医院 | 国产精品爱久久久久久久 | 精品啪啪 | 北条麻妃国产九九九精品小说 | 天堂精品视频 | 日韩免费av | 久久亚洲一区 | 中文字幕一区二区三区不卡在线 | 欧美一区二区三区 | 91一区二区在线观看 | 91高清在线观看 | 国产一区二区三区亚洲 | 久久精品国产亚洲夜色av网站 |