0

私はuiscrollview、2つのtextviewfieldsを持っており、キーボードでtextviewを非表示にすることに関するすべての投稿を読み、多くの方法を試しましたが、それでも非表示になっています。

私のコードは次のようなものです:ViewController.h

@property(nonatomic,retain) IBOutlet UITextView *campaignTitle;
@property (weak, nonatomic) IBOutlet UITextView *campaignDescription;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property UITextView *activeTextView;

ViewController.m

@synthesize campaignTitle;
@synthesize campaignDescription;
@synthesize scrollView;
@synthesize activeTextView;

#define SCROLLVIEW_CONTENT_HEIGHT 460
#define SCROLLVIEW_CONTENT_WIDTH  320

- (void)viewDidLoad
{
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    scrollView.contentSize=CGSizeMake(0, self.view.frame.origin.y+self.view.frame.size.height+50);
    [super viewDidLoad];
}

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    textView.text = @"";
    self.activeTextView = textView;
    return YES;
}

- (BOOL) textViewShouldEndEditing:(UITextView *)textView
{
     self.activeTextView = nil;
    return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
        [textView resignFirstResponder];
    return YES;
}

- (void)keyboardWasShown:(NSNotification *)notification
{   
    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, activeTextView.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeTextView.frame.origin.y - (keyboardSize.height-15));
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

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

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

- (IBAction)dismissKeyboard:(id)sender
{
    [self.activeTextView resignFirstResponder];
}

スクロールビューとcampaignTitle、campaignDescriptionからの委任があります。

なにが問題ですか?キーボードがまだ下部のuitextviewを隠しているのはなぜですか?表示される可能性のある追加のコードは、UITextViewの[戻る]ボタンをサポートすることです。

4

2 に答える 2

2

または、TPKeyboardAvoidingを使用します。それはうまく機能していて、セットアップがとても簡単です:

  1. UIScrollViewをViewControllerのxibに追加します
  2. スクロールビューのクラスをTPKeyboardAvoidingScrollViewに設定します(IDインスペクターを介してまだxibにあります)
  3. そのスクロールビュー内にすべてのコントロールを配置します

また、キーボードの[次へ]ボタンを自動的に接続して、テキストフィールドを切り替えます。

于 2014-02-12T22:22:11.597 に答える
1

UITextFieldsが実際にストーリーボード内のUIScrollView内にあり、誤ってその下のUIViewに配置されていないことを確認してください。

setContentOffsetは、意図した効果ではない場合でも、何かを実行する必要があります。何もしていないように見えるので、UITextFieldsがそのサブビューではないか、UIScrollViewがInterfaceBuilderに接続されていないのではないかと思います。

于 2012-09-10T20:49:30.380 に答える