简体   繁体   中英

UIScrollView not scrollable using autolayout and dynamic constraints

I have a view which contains a scrollview, a containerview and 3 subviews (topview, textview and bottomview). See the view's hierachy below. The height of the textview is defined based on its content. I defined a dynamic constraint for its height and I change it in viewDidLayoutSubviews. The size of the textview is correct but the problem is that my scrollview is not scrolling. What I am doing wrong? Perhaps I need to add/modify some of the other constraints?

在此处输入图片说明

-(void)viewDidLayoutSubviews
{
    NSAttributedString * string = [[NSAttributedString alloc] initWithString:self.game.description];
    CGFloat heightTV = [self textViewHeightForAttributedText:string andWidth:260];
    self.dynamicTVHeight.constant = heightTV;
    [self.view layoutIfNeeded];
}

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
    UITextView *textView = [[UITextView alloc] init];
    [textView setAttributedText:text];
    CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

The UIScrollView scroll only if it has contentSize bigger than bounds. Content size of UIScrollView can be set automatically by calculating frames of subviews (calculated from constraints) or manually in code (usually in viewDidLoad). Double check your UIScrollView's contentSize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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