[英]UITextView setContentOffset:CGPointZero in layoutSubviews doesn't work in iOS8
I have a PopupView
that extends UIView
. 我有一个扩展UIView
的PopupView
。 In PopupView
I have a UITextView
. 在PopupView
我有一个UITextView
。
When the PopupView
show, my UITextView
doesn't start at first line (it scroll a little bit to bottom) 当PopupView
显示时,我的UITextView
不会从第一行开始(它向下滚动一点点)
So I use the code below to scroll the UITextView
to top after PopupView
appears 所以我使用下面的代码在PopupView
出现后将UITextView
滚动到顶部
- (void)layoutSubviews{
[super layoutSubviews];
[self.contentTextView setContentOffset:CGPointZero animated:NO];
}
It works well in iOS9 (both device and simulator) but it doesn't work in iOS8 它在iOS9(设备和模拟器)中运行良好,但在iOS8中不起作用
Any idea to fix it. 任何想法来解决它。 Any help would be great appreciated 任何帮助将非常感谢
UPDATE UPDATE
I found that drawRect
get called after layoutSubviews and if I setContentOffset:CGPointZero
inside it, it will work 我发现drawRect
在layoutSubviews之后被调用,如果我在其中setContentOffset:CGPointZero
,它将工作
-(void)drawRect:(CGRect)rect{
[self.contentTextView setContentOffset:CGPointZero];
}
But I found the purpose of drawRect
: 但我找到了drawRect
的目的:
drawRect: - Implement this method if your view draws custom content. drawRect: - 如果您的视图绘制自定义内容,请实现此方法。 If your view does not do any custom drawing, avoid overriding this method. 如果您的视图没有执行任何自定义绘图,请避免覆盖此方法。
Is it good to use drawRect
without layoutSubviews
in my case? 在我的情况下使用没有layoutSubviews
drawRect
是否layoutSubviews
?
According to @longpham instruction, the drawRect()
will use GPU so it's not good. 根据@longpham指令, drawRect()
将使用GPU,因此它并不好。 Here is the solution that solve my problem 这是解决我的问题的解决方案
-(void)awakeFromNib{
[super awakeFromNib];
[self layoutIfNeeded]; // call layoutIfNeeded here to make layoutSubviews get called whenever layout change
}
- (void)layoutSubviews{
...
[self.contentTextView setContentOffset:CGPointZero];
}
it worked for me 它对我有用
- (void)drawRect:(CGRect)rect {
self.textView=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self addSubview:self.textView];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.