0

I am creating a customview which is a subclass of UIScrollView. I am adding UIButtons inside UIScrollView dynamically along with images and actions. But scrollview is not scrolling horizontally with buttons. I know this topic is discussed many times on SO. I tried all posts posted on SO, but not getting expected result. Any suggestion about where i am going wrong will be helpful.

MyCustomView.h

@interface MyCustomView : UIScrollView

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
+ (id)customView;

@end

MyCustomView.m

@implementation MyCustomView
@synthesize scrollView;

+ (id)customView
{
    MyCustomView *customView = [[[NSBundle mainBundle]  loadNibNamed:@"MyCustomView" owner:nil options:nil] lastObject];


    if ([customView isKindOfClass:[MyCustomView class]])
        return customView;
    else
        return nil;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [self setupHorizontalScrollView];
}

- (void)setupHorizontalScrollView
{
    //scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor blueColor]];
    [scrollView setCanCancelContentTouches:YES];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = NO;
    scrollView.scrollEnabled = YES;
    //scrollView.pagingEnabled = YES;

    CGFloat cx = 0;
    NSArray *buttons = @[@{@"Tag":@1,@"Image":[UIImage imageNamed:@"borderImage1.png"]},
                         @{@"Tag":@2,@"Image":[UIImage imageNamed:@"borderImage2.png"]},
                         @{@"Tag":@3,@"Image":[UIImage imageNamed:@"borderImage3.png"]},
                         @{@"Tag":@4,@"Image":[UIImage imageNamed:@"borderImage4.png"]},
                         @{@"Tag":@1,@"Image":[UIImage imageNamed:@"borderImage1.png"]},
                         @{@"Tag":@2,@"Image":[UIImage imageNamed:@"borderImage2.png"]},
                         @{@"Tag":@3,@"Image":[UIImage imageNamed:@"borderImage3.png"]},
                         @{@"Tag":@4,@"Image":[UIImage imageNamed:@"borderImage4.png"]},
                         @{@"Tag":@1,@"Image":[UIImage imageNamed:@"borderImage1.png"]}
                         ];

    // CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
    for (NSDictionary *dict in buttons)
    {
        UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect rect = button.frame;
        rect.size.height = 40;
        rect.size.width = 40;
        rect.origin.x = cx;
        rect.origin.y = 0;

        button.frame = rect;
        button.tag = [dict[@"Tag"] integerValue];
        [button setImage:dict[@"Image"]
                forState:UIControlStateNormal];
      //  [button addTarget:self action:@selector(buttonAction:)
      //   forControlEvents:UIControlEventTouchUpInside];

        [self.scrollView addSubview:button];
        //frame.origin.x+=frame.size.width+20.0f;
        cx += button.frame.size.width+5;
    }

    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
@end      

I went through below SO links, but didn't get solution.

LINK1 LINK2LINK3 LINK4

Community
  • 1
  • 1
user2533604
  • 655
  • 1
  • 11
  • 28
  • is that scrollView property another scrollView inside your scrollView or is it an not really needed reference to self? – robert Jul 23 '14 at 11:42
  • print "CX" value and see , What does you get .. If it's greater than scrollview size than it will work other wise not scroll – Jogendra.Com Jul 23 '14 at 11:42

3 Answers3

0

To be able to scroll you need to keep a view over scrollview that goes out of the bound of the screen. So drag a view and enlarge it beyong the device bounds so that it would be able to hold your subviews.

Hem Acharya
  • 258
  • 3
  • 15
0

Its better to use UICollectionView for this kind of a problem. It is easier to implement and also will help you do all necessary manipulations.

NKB
  • 651
  • 5
  • 13
0

Your MyCustomView is a subClass of UIScrollView. So you don't need to add UIScrollView property to your class. Delete scrollView property and replace all self.scrollView with self

Shamsiddin Saidov
  • 2,281
  • 4
  • 23
  • 35