【问题标题】:UIScrollView and still the keyboard hides the textUIScrollView 并且键盘仍然隐藏文本
【发布时间】:2012-09-10 20:24:24
【问题描述】:

我有 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];
}

有来自滚动视图和活动标题、活动描述的委托。

怎么了?为什么键盘仍然隐藏底部的uitextview?您可能看到的额外代码是支持 UITextView 中的 Return 按钮

【问题讨论】:

    标签: objective-c xcode uiscrollview uitextview


    【解决方案1】:

    或使用TPKeyboardAvoiding。 它工作得很好,而且很容易设置:

    1. 将 UIScrollView 添加到视图控制器的 xib 中
    2. 将滚动视图的类设置为 TPKeyboardAvoidingScrollView(仍然 在 xib 中,通过身份检查器)
    3. 将所有控件放在该滚动视图中

    它还会自动连接键盘上的“下一步”按钮来切换文本字段。

    【讨论】:

    • 谢谢!似乎有些倒退,iOS 不能自己解决这个问题。
    【解决方案2】:

    确保您的 UITextFields 实际上位于情节提要内的 UIScrollView 内,而不是意外放置在其下方的 UIView 上。

    setContentOffset 应该做一些事情,即使它不是预期的效果。由于它似乎什么也没做,我怀疑您的 UITextFields 不是它的子视图,或者您的 UIScrollView 没有在界面生成器中连接。

    【讨论】:

      猜你喜欢
      • 2011-03-30
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2011-01-19
      • 2019-06-08
      • 2014-03-19
      相关资源
      最近更新 更多