iOS - UIScrollView is not working (it doesn't

2019-02-01 20:45发布

I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.

When I compile the application I get no scrolling at all. The view stays fixed.

What I did:

  1. Added an UIScrollView via Interface Builder to my view.
  2. Added an UIImageView via Interface Builder to my view.
  3. Verified that UIImageView is below UIScrollView in Interface Builder.
  4. Set the size of the UIScrollView with ViewDidLoad.

How do I do this?

Code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView.contentSize = CGSizeMake(320, 1250);
}

Screenshots:

ImageView:

Enter image description here

ScrollView:

Enter image description here Enter image description here

Enter image description here

17条回答
聊天终结者
2楼-- · 2019-02-01 21:03

From you last screenshot and from your comments it looks like your scrollView is way to big. The scrollview must be visible on screen completely. For example a full screen UIScrollView on iPhone would have a size of 320 x 460.

If the scrollview is the same size as its content you can't scroll.

The greenish rectangle shows the size of your scrollview, the pinkish the size of your content (your image):

enter image description here

查看更多
霸刀☆藐视天下
3楼-- · 2019-02-01 21:04

Don't forget to add the category protocol to the interface, like this

@interface MyViewController : <UIScrollViewDelegate>

If you don't, you will not be able to set the scroll view delegate to self

(i.e. [MyScrollView setDelegate:self];)

If you do this, it should work.

My code is:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [contentScrollView setDelegate:self];
    [contentScrollView setScrollEnabled:YES];
    contentScrollView.contentSize = CGSizeMake(310, 500);
    contentScrollView.frame = CGRectMake(5, 188, 310, 193);
}
查看更多
smile是对你的礼貌
4楼-- · 2019-02-01 21:08

Since Xcode 5 it does not work like before. Also scrolling to the end of a scrollable text field makes problems. There are also differences between doing it on iPhone or iPad. On iPhone it worked without delayed timer.

This worked for me:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSTimer *timerforScrollView;
    timerforScrollView =[NSTimer scheduledTimerWithTimeInterval:0.1
           target:self selector:@selector(forScrollView)userInfo:nil repeats:NO];   
}

- (void) forScrollView {
    [scrollviewPad setScrollEnabled:YES];
    [scrollviewPad setContentSize:CGSizeMake(768, 1015)]; // must be greater then the size in Storyboard
}
查看更多
爷的心禁止访问
5楼-- · 2019-02-01 21:08

I had the same issue and was looking for the answer in this thread. I tried all the stuff, but nothing works. Then I found this:

deselect Use Auto Layout.

You just need to deselect "Use Auto Layout" in File Inspector of your ViewController. Ta-Da, it works immediately for me. Enjoy.

查看更多
祖国的老花朵
6楼-- · 2019-02-01 21:09

Swift 4, iOS 11

The simplest way using autolayout:

  1. Add UIScrollView and pin it 0,0,0,0 to superview (or your desired size)
  2. Add UIView in ScrollView, pin it 0,0,0,0 to all 4 sides and center it horizontally and vertically.
  3. In size inspector, change 'bottom' and 'align center Y' priority to 250.
  4. Add all views that you need into this view. Don't forget to set the bottom constraint on the lowest view.
查看更多
劫难
7楼-- · 2019-02-01 21:10

Did you assign the scroll's view delegate? Always remember these:

[self.scrollView setDelegate:self]; 
[self.scrollView setScrollEnabled:YES];
查看更多
登录 后发表回答