UIScrollView not scrolling regardless of large contentSize

前端 未结 5 1979
予麋鹿
予麋鹿 2020-12-09 05:58

I have created a UIScrollView and added subviews to it in Interface Builder, but I am not able to get it to scroll. I have looked at other similar questions, but I have set

5条回答
  •  悲&欢浪女
    2020-12-09 06:35

    Try this code to auto set content size

    first copy and paste your view controller in viewDidLoad Method

     [self MHAutoContentSizeForScrollViewWithPadding:10 scrl:YOUR_SCROLLVIEW];
    

    then copy and paste in below viewDidLoad Method

    -(void)MHAutoContentSizeForScrollViewWithPadding:(CGFloat)padding scrl:(UIScrollView*)view{
        if ([view isKindOfClass:[UIScrollView class]]) {
            CGRect rect = CGRectZero;
            for(UIView * view in YOUR_SCROLLVIEW.subviews){
                rect = CGRectUnion(rect, view.frame);
            }
            [YOUR_SCROLLVIEW setContentSize:CGSizeMake(rect.size.width, rect.size.height+padding)];
        } else {
            NSLog(@"You can only set the ContentSize for ScrollViews");
        }
    }
    

    connect YOUR_SCROLLVIEW delegate also and TextFeild Delegate also Happy Coding!!!

提交回复
热议问题