3

I got a problem about UIScrollView. I am making a custom view which inherits UIView. The view has a UIScrollView on which there are lots of buttons which should scroll left and right. The UIScrollView and buttons can show normally. But I cannot scroll the buttons. Could someone give me some suggestions? Thanks a lot!

MZMPhotoCalenderSwitcher.h

#import <UIKit/UIKit.h>

@interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *topSwitcher;

@end

MZMPhotoCalenderSwitcher.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
    self.topSwitcher.backgroundColor = [UIColor greenColor];
    self.topSwitcher.pagingEnabled = YES;
    self.topSwitcher.showsHorizontalScrollIndicator = NO;
    self.topSwitcher.showsVerticalScrollIndicator = NO;

    [self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}

- (void)add:(int)num ButtonsOnView:(UIScrollView *)view withButtonWidth:(CGFloat)width andHeight:(CGFloat)height
{
    CGFloat totalTopSwitcherWidth = num * width;
    [view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];
    CGFloat xOffset = 0.0f;

    for (int i=1; i<=num; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:CGRectMake(xOffset, 0, width, height)];
        xOffset += width;
        [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];

        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [button setTag:i];
        [button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];

        if (i % 2 == 0)
            [button setBackgroundColor:[UIColor yellowColor]];
        else
            [button setBackgroundColor:[UIColor redColor]];

        [view addSubview:button];
    }
}
Preston Cheung
  • 175
  • 1
  • 2
  • 12
  • I modify `[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f]; }` to `[self add:10 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f]; }` , then print the contentSize.width after `[view addSubview:button];`. The contentSize.width = 448 which is correct. Because each button width is 44.8 and there are 10 buttons. But I don't understand why the scrollview still doesn't scroll. Furthermore, I implement the function buttonEvent which just print a message. When I click the button, the log doesn't show either. I think I must miss something. – Preston Cheung Oct 25 '13 at 03:27

10 Answers10

5

Below following line

[view addSubview:button];

add

view.contentSize = CGSizeMake(view.contentSize.width, button.frame.origin.y + button.frame.size.height);

This will set content size of scroll view to the bottom of your button.

Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
  • Thanks a lot. But it still doesn't work after adding your code. By the way, why cannot I calculate and set contentSize like this: [view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)]; I think that I just calculate and set enough contentSize before adding buttons to the scrollview. – Preston Cheung Oct 25 '13 at 02:37
  • @PrestonCheung Are you adding only 3 buttons on scroll view. If yes then you will not be able to scroll. Instead of 3 try to add 30 buttons on scroll view and it will work. What happening here is that content size of topSwitcher scroll view is less than its bounds hence it is not scrolling. – Akshay Nalawade Oct 25 '13 at 05:24
  • Yes, you are right! If I add only 3 buttons it doesn't scroll, but if I add 30 buttons it works normally. – Preston Cheung Oct 25 '13 at 05:46
1

It should be issue of content size, also check that scrollEnabled property of UIScrollView object is set to TRUE,

Check solution of UIScrollView won't scroll!

Community
  • 1
  • 1
Vishwa Patel
  • 484
  • 2
  • 10
1

Just disable "Auto Layout" in your project and it will start working!

AJ112
  • 5,291
  • 7
  • 45
  • 60
  • Just disable it on the xib where it is not necessary, and leave it enabled where you really need to adjust the layout at runtime. – Massimo Feb 11 '15 at 14:26
1

I'm using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:

  1. The scroll view content size height MUST be bigger than the screen height, this almost goes without saying...

  2. The top/bottom/left/right constraint of the scroll view HAS TO BE pinned, I did this using storyboard so no code showing up here

If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.

This worked for me, hope it can help someone.

dulan
  • 1,584
  • 6
  • 22
  • 50
1

Combining all of this advise into one simple answer:

// In viewDidLoad

// set the size of the scrollview (in this case I made it the size of its container
self.scrollView.frame = self.view.frame;
// now set the size of the content to be scrolled within the scrollview
// the content to be displayed must be bigger than the scrollView
// in this case I added 100 to make the content size, so that it is taller than the height of the scrollView
// now it scrolls
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 100);
Guy
  • 361
  • 2
  • 14
0

Where are you add ScrollView on SuperVIew? And where are you recalculate Content size property of scrollView?

[view setContentSize:CGSizeMake(<summYourViews.width>,<summYourViews.height>)];
Bimawa
  • 3,535
  • 2
  • 25
  • 45
0

Your problem is that you haven't set your contentSize, therefore, the contentSize is the same than the frame view.

You need to do this in your viewDidLoad

- (void)viewDidLoad {
    ........
    self.scrollView.contentSize = CGSizeMake(/*yourWidth*/, /*yourHeight/*);
    ........
}

Try do adjust your contentSize to the last position of your object plus its size plus a padding.

Alex
  • 1,061
  • 10
  • 14
0

You have to set scrollview frame size and content size.

Pravin
  • 44
  • 9
  • I set the scrollView frame size `self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];` and the content size `[view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];`. But it doesn't work. – Preston Cheung Oct 25 '13 at 03:34
  • content size and frame size could not be same,content size should greater than frame size. – Pravin Oct 25 '13 at 06:27
0

try this . . . .

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
    self.topSwitcher.backgroundColor = [UIColor greenColor];
    self.topSwitcher.pagingEnabled = YES;
    self.topSwitcher.showsHorizontalScrollIndicator = NO;
    self.topSwitcher.showsVerticalScrollIndicator = NO;
    [topSwitcher setContentSize:CGPointMake(self.view.bounds.size.width,TOP_SWITCHER_HEIGHT+400)];

    [self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
0

It's amazing.

I just move the codes which was in viewDidLoad to initWithFrame:, then it works. I do not know why it is?

Preston Cheung
  • 175
  • 1
  • 2
  • 12