iOS ScrollView needs constraint for y position or height

前端 未结 8 1641
栀梦
栀梦 2020-11-30 18:21
-ViewController
--View
---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0)
----ContentView (Top,Bottom,Leading,Trailing spaces to superview set          


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 19:10

    It will work

    -ViewController
    --View
    ---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0)
    ----ContentView (Top,Leading,Trailing spaces to ScorllView set to 0 | Height to dynamic (1000))
    -----Label1
    -----etc...
    

    Do same for your ContentView , Your Own Calculation for your Content

    - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:YES];
    [self adjustHeightOfTableview];
    }
    
    - (void)adjustHeightOfTableview
    {
    CGFloat height = tblMainMenu.contentSize.height;
    CGFloat maxHeight = tblMainMenu.superview.frame.size.height - tblMainMenu.frame.origin.y;
    
    // if the height of the content is greater than the maxHeight of
    // total space on the screen, limit the height to the size of the
    // superview.
    
    if (height > maxHeight)
        height = maxHeight;
    
    // now set the height constraint accordingly
    self.tableViewHeightConstraint.constant = height;
    [UIView animateWithDuration:0.1 animations:^{
        [self.view setNeedsUpdateConstraints];
    }];
    }
    

    Edit 1

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
        NSString *OptName_length = @" Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam";
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0]};
        CGRect rect = [OptName_length boundingRectWithSize:CGSizeMake(tableView.bounds.size.width-150.0, MAXFLOAT)
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:attributes
                                                      context:nil];
    
        CGSize requiredSize = rect.size;
        return requiredSize.height +5.0f;
        }
    

    Edit 2

    Check the site, this will help you... uitableviewcell-dynamic-height-with-autolayout-conclusion

提交回复
热议问题