且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尽管 contentSize 小于 UIImageView,但 UIScrollView 不滚动

更新时间:2023-11-18 08:00:34

那是因为你的滚动视图边界的大小大于你的内容大小.内容大小需要是图像视图的实际大小,可见大小由框架/边界设置.我猜你想在 {200, 200} 大小的矩形中滚动图像?试试这个:

That's because that your size of bounds of scroll view is larger than your content size. The content size need to be the actual size of image view, and the visible size is set by frame / bounds. I guess you want to scroll a image in a {200, 200} sized rect? Try this:

        UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // this is your visible rect
        [imgScrollView setScrollEnabled:YES];
        [imgScrollView setClipsToBounds:YES];
        [imgScrollView addSubview:imgView];
        [imgScrollView setBackgroundColor:[UIColor yellowColor]];

        [imgScrollView setContentSize:imgFrame.size]; // this is your image view size