Scrollview will not scroll even with larger conten

2019-06-02 17:32发布

Got a strange problem which I dont understand.

I set the content size to a CGSize I created called newSize. I am adding buttons in a row in the UIScrollView that goes outside of the frame of the scrollview and changing the content size to match the new width + 400 for insurance.

    newSize.width = contentSizeWidth+400;

    NSLog(@"newSize width: %f", newSize.width);

    [_scrollViewOutlet addSubview:button];

    [_scrollViewOutlet setContentSize:newSize];

    NSLog(@"scrollviewOutlet Content Width: %f", _scrollViewOutlet.contentSize.width);

the NSLog prints out 768. scrolling is enabled in the scrollview in interface builder but I cannot scroll the scrollview? What could the problem be?

3条回答
forever°为你锁心
2楼-- · 2019-06-02 17:50

I resolved the issue. It was an autolayout tick box that needed unticking in the view.

查看更多
我命由我不由天
3楼-- · 2019-06-02 18:05

This may help: add buttons before you provide contentSize.

// add all buttons
// create contentSizeWidth presumably by adding all buttons height
contentSizeWidth = contentSizeWidth + buttonHeight;// repeat this for every button  
 [_scrollViewOutlet addSubview:button];

// create newSize
newSize.width = contentSizeWidth+400;

// set content size
  [_scrollViewOutlet setContentSize:newSize];
查看更多
何必那么认真
4楼-- · 2019-06-02 18:06

Mayble the proble is that the width of the frame is equal to the width of the contentSize.

查看更多
登录 后发表回答