iphoneiosipadcocoauitoolbar

How to add scrolling for buttons on UIToolbar?


How to add scrolling for UIBarButtonItem buttons on UIToolbar (to place many buttons on the toolbar)?

buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonDoneDown)];
NSArray *itemsArray = [NSArray arrayWithObjects:buttonDone, nil];
[toolbar setItems:itemsArray];

Thanks a lot for the help!


Solution

  • Replace superView of the toolbar:

    buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonDoneDown)];
    NSArray *itemsArray = [NSArray arrayWithObjects:buttonDone, nil];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = toolbar.frame;
    scrollView.bounds = toolbar.bounds;
    scrollView.autoresizingMask = toolbar.autoresizingMask;
    scrollView.showsVerticalScrollIndicator = false;
    scrollView.showsHorizontalScrollIndicator = false;
    //scrollView.bounces = false;
    UIView *superView = toolbar.superview;
    [toolbar removeFromSuperview];
    toolbar.autoresizingMask = UIViewAutoresizingNone;
    toolbar.frame = CGRectMake(0, 0, X, toolbar.frame.size.height);
    toolbar.bounds = toolbar.frame;
    [toolbar setItems:itemsArray];
    scrollView.contentSize = toolbar.frame.size;
    [scrollView addSubview:toolbar];
    [superView addSubview:scrollView];