带有文本框的故事板滚动视图 [英] Storyboard scroll view with text boxes
问题描述
我正在故事板中创建一个视图,该视图有一堆底部带有按钮的文本框.当我单击文本框时,键盘出现.这隐藏了我的一些文本框.所有文本框都位于滚动视图中.有没有办法在故事板中(如果没有,则在代码中)使滚动视图在键盘出现时在所有内容中正确滚动?
I am creating a view in a storyboard that has a bunch of text boxes with a button at the bottom. When I click in the text box the keyboard appears. This hides some of my text boxes. All text boxes live within the scrollview. Is there a way in storyboarding (if not then in code) to make the scrollview scroll properly amongst all the content when the keyboard appears?
我是否必须在键盘出现时动态更改内容大小,或者有没有办法设置滚动视图,以便在键盘出现时自动调整其大小.
Do I have to dynamically change the content size when the keyboard appears, or is there a way that I can set up the scroll view such that it automagically gets resized when the keyboard appears.
推荐答案
我想我和你有同样的问题,我所做的是在键盘出现或隐藏时动态更改滚动视图的 contenSize 属性.
I think I had the same problem as you, and what I did was to dynamically change the contenSize property of my scroll view when the keyboard appears or hides.
我在管理我的滚动视图的 ViewController 类中订阅了键盘通知:
I subscribed for keyboard notifications in the ViewController class that manages my scrollview:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
然后在目标方法中调整大小:
And then do the resizing in the target method:
- (void)keyboardWillShow:(NSNotification *)note{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//This is the value of the rect where the keyboard is drawn, and may come with height and width swapped
CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | curve
animations:^{
yourScrollableView.frame = ......
}
completion:nil];
}
当然这同样适用于键盘隐藏.
Of course the same applies to keyboard hiding.
这篇关于带有文本框的故事板滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!