我使用了以下代码,但无法获得结果
CGRect newFrame = _text_view.frame;
CGRect newToolbarFrame = self.navigationController.toolbar.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
从上面的代码中,请让我知道什么是newsize?
为了根据内容向上扩展textview的高度,我使用了gitHub项目。
为了简单地更改文本视图的位置,使其保持在键盘上方,请尝试以下操作
// First add these
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后在这些方法中,执行此操作
- (void) keyboardWillShow:(NSNotification *)note {
//Get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
// get a rect for the textView frame
CGRect containerFrame = _text_view.frame;
// Provided you keep fixed number of maximum lines
containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// set views with new info
_text_view.frame = containerFrame;
// Change toolbar location as per it's height
_tool.frame=CGRectMake(0,
_text_view.frame.origin.y - _tool.frame.size.height,
_tool.frame.size.width,
_tool.frame.size.height);
}
- (void) keyboardWillHide:(NSNotification *)note{
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
// get a rect for the textView frame
CGRect containerFrame = _text_view.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// set views with new info
_text_view.frame = containerFrame;
// Similarly again change toolbar location
}
或者,您可以简单地将textview保留在scrollView中,并在键盘显示时更改滚动视图的插图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句