简体   繁体   English

在调用viewDidAppear方法之前,iOS7获取UITextView contentSize吗?

[英]IOS7 get UITextView contentSize before viewDidAppear method is called?

I am upgrading my app from iOS6 to iOS7 and I am having some issues with my UIViewControllers layout. 我正在将我的应用程序从iOS6升级到iOS7,并且UIViewControllers布局存在一些问题。 In iOS6 I was able to resize my UITextViews dynamically by getting the contentSize property of the textView and then setting its frame property. 在iOS6中,我能够通过获取textView的contentSize属性然后设置其frame属性来动态调整UITextViews的大小。 I would do all the resizing in the viewWillAppear method so that way the view would be resized before it was visible. 我将在viewWillAppear方法中进行所有调整大小,以便在可见之前调整视图的大小。 Now in iOS7 it doesn't work. 现在在iOS7中不起作用。 The only way the contentSize property works is if I set in the viewDidAppear method. contentSize属性工作的唯一方法是在viewDidAppear方法中设置。 I hate doing that because it causes the view to jump after it's already visible. 我讨厌这样做,因为它会导致视图在可见后跳转。 Has anyone figured out how to solve this problem? 有没有人想出如何解决这个问题?

Here is my code that no longer works in iOS7: 这是我的代码在iOS7中不再起作用:

-(void)viewWillAppear:(BOOL)animated
{
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(5, 0, 310, 0)];
    self.textView.text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
    [self.view addSubview:self.textView];

    CGRect textViewframe;
    textViewframe = self.textView.frame;
    textViewframe.size.height = [self.textView contentSize].height;
    self.textView.frame = textViewframe;
}

You can call [self.textView layoutIfNeeded] after creating it with the appropriate frame (so it can figure out its max width) then contentSize should be representing the content. 您可以在使用适当的帧创建[self.textView layoutIfNeeded]之后调用它(以便可以确定其最大宽度),然后contentSize应该表示内容。

Also: If textView is defined as weak , the text view will be gone on the next line (as ARC adds a release and the optimizer moves it up). 另外:如果将textView定义为weak ,则文本视图将在下一行消失(因为ARC添加了发行版,而优化器将其向上移动)。 They announced this optimization change for Debug (-O0) builds during WWDC. 他们宣布了在WWDC期间针对Debug(-O0)版本进行的优化更改。

I met a similar situation as urs. 我遇到过与urs类似的情况。 Mine shows with a different bug but due to the same reason: the contentSize property is silently changed by iOS7 incorrectly. 我的游戏显示了一个不同的错误,但由于相同的原因:iOS7默默地更改了contentSize属性。 Here is how I work around it. 这是我的解决方法。 Its kinda a ugly fix. 这有点丑陋。 Whenever I need to use textView.contentSize, I calculate it by myself. 每当我需要使用textView.contentSize时,我都会自己计算。

-(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
{
    CGSize size = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    return size;
}

then you can just call this function to get the size: 那么您可以调用此函数以获取大小:

CGSize cont_size =   [self sizeOfText:self.text widthOfTextView:self.frame.size.width withFont:[UIFont systemFontOfSize:15]];

then, don't do the following: 然后,请不要执行以下操作:

self.contentSize = cont_size;// it causes iOS halt occasionally.

so, just use cont_size directly. 因此,只需直接使用cont_size。 I believe it's bug in iOS7 for now. 我认为目前是iOS7中的错误。 Hopefully apple will fix it soon. 希望苹果会尽快修复它。 Hope this is helpful. 希望这会有所帮助。

We have the same issue with table views. 表格视图存在相同的问题。 What is even worse here is that moving to a later lifecycle call like ViewDidAppear may worsen the user experience (from a performance point of view). 更糟糕的是,移至更高的生命周期调用(如ViewDidAppear)可能会恶化用户体验(从性能角度而言)。 We try to split the code up and do only the content size setting in ViewWillAppear. 我们尝试拆分代码,并仅在ViewWillAppear中进行内容大小设置。 But the UI issues then remain. 但是,UI问题仍然存在。

Having this problem with UITextView as well and I can't depend on contentSize giving the right value to resize my textView, nor does calling layoutIfNeeded on the textView help to get the right value for me. UITextView也有此问题,我不能依靠contentSize提供正确的值来调整textView的大小,也不能依靠在textView上调用layoutIfNeeded来帮助我获取正确的值。 So to solve it, I use sizeThatFits instead of sizeWithFont to figure out UITextView contentSize. 因此,为了解决该问题,我使用sizeThatFits而不是sizeWithFont来确定UITextView contentSize。 SizeWithFont has some subtle differences and sizeThatFits is more proper to use for UITextViews, see UITableViewCell with UITextView height in iOS 7? SizeWithFont有一些细微的差别,并且sizeThatFits更适合用于UITextViews,请参阅iOS 7中具有UITextView高度的UITableViewCell吗? .

So my solution looks more like: 所以我的解决方案看起来更像:

UITextView* sizingView      = [[UITextView alloc] initWithFrame:textView.frame];
sizingView.attributedText   = textView.attributedText;
CGSize contentSize          = [sizingView sizeThatFits:CGSizeMake(sizingView.frame.size.width, FLT_MAX)];

If you'll notice, I create a new UITextView, copy the attributed text, and get the content size there because unfortunately sizeThatFits has some nasty side effects which can cause the textView not to update properly in iOS 7.0. 如果您会注意到,我会创建一个新的UITextView,复制属性文本,并在那里获取内容大小,因为不幸的是,sizeThatFits具有一些讨厌的副作用,这些副作用可能导致textView在iOS 7.0中无法正确更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM