具有滚动动态设置的UITextView调整为默认值 [英] UITextView with scroll dynamically set resizes to default
问题描述
我已经实现了一个ViewController,其底部 UIView
包含 UITextView
,其中滚动禁用,在您键入时调整大小内部。
I've implemented a ViewController that has a bottom UIView
containing UITextView
with scroll disabled that resizes as you type inside.
当包含的文字高度达到90像素时,我启用滚动 - >
When the height of the text contained reaches 90 pixels, I enable scroll ->
scrollEnabled = YES;
应该发生什么: UITextView
及其 superview
应保持为限制的高度(超过90像素限制)。
What's supposed to happen: The UITextView
and its superview
should stay as the height that they were limited to (over the 90 pixel limit).
会发生什么: UITextView
调整回其默认值。
更多信息:
我使用 Multiline UITextField 的代码作为我的底层视图。
我正在使用iOS7。
More Info: I'm using the code of Multiline UITextField as my bottom view. I'm using iOS7.
感谢所有帮助。谢谢。
编辑:我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.textBox.scrollEnabled = NO;
self.textBox.font = [UIFont fontWithName:@"Helvetica" size:14];
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(keyPressed:)
name: UITextViewTextDidChangeNotification
object: nil];
}
- (void)keyboardWasShown:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[self setViewMovedUp:YES byHeight:kbSize.height];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[self setViewMovedUp:NO byHeight:kbSize.height];
}
- (void)keyPressed:(id)sender
{
CGRect textRect = [self.textBox.text boundingRectWithSize:CGSizeMake(255,MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:14]}
context:nil];
NSInteger newSizeH = textRect.size.height;
if (self.textBox.hasText) {
// if the height of our new chatbox is
// below 90 we can set the height
if (newSizeH <= 90) {
self.textBox.scrollEnabled = NO;
[self.textBox scrollRectToVisible:CGRectMake(0,0,1,1)
animated:NO];
// chatbox
CGRect chatBoxFrame = self.textBox.frame;
chatBoxFrame.size.height = newSizeH + 12;
self.textBox.frame = chatBoxFrame;
// form view
CGRect formFrame = self.commentBox.frame;
formFrame.size.height = 30 + newSizeH;
self.commentBox.frame = formFrame;
}
// if our new height is greater than 90
// sets not set the height or move things
// around and enable scrolling
if (newSizeH > 90) {
self.textBox.scrollEnabled = YES;
CGRect frame = self.textBox.frame;
frame.size.height = 102;
self.textBox.frame = frame;
CGRect formFrame = self.commentBox.frame;
formFrame.size.height = 30 + 90;
self.commentBox.frame = formFrame;
}
}
}
- (void)setViewMovedUp:(BOOL)movedUp byHeight:(CGFloat)height
{
int movement = movedUp ? -height : height;
[UIView animateWithDuration:0.3
animations:^{
self.dataView.frame = CGRectOffset(self.dataView.frame, 0.0, movement);
}];
}
推荐答案
虽然我有点晚了,但只是因为。
我自己也遇到了同样的问题。滚动时文本视图恢复为原始大小。我解决它的方法是更新UITextView上的高度约束。
这是每次更新UITextView的大小时,您还需要更新相应的约束。
Although I am a little late, but just incase. I faced the same problem myself. The text view came back to the original size on scroll. The way I resolved it was to update the height constraint on the UITextView. That is each time you update the size of the UITextView, you also need to update the corresponding constraints.
这篇关于具有滚动动态设置的UITextView调整为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!