我有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中的Return按钮
答案 0 :(得分:2)
或使用TPKeyboardAvoiding。 它工作得很好,而且很容易设置:
它还会自动连接键盘上的“下一步”按钮以切换文本字段。
答案 1 :(得分:1)
确保您的UITextField实际位于故事板内的UIScrollView内部,并且不会意外地放在其下方的UIView上。
setContentOffset应该执行SOMETHING,即使它不是预期的效果。由于它似乎什么都不做,我怀疑你的UITextFields不是它的子视图,或者你的UIScrollView没有在接口构建器中连线。