In your .h file

#define MY_SCROLL_BUTTON_WIDTH		107
@interface ViewController : UIViewController {
}
@property (weak, nonatomic) IBOutlet UIScrollView *MyScrollView;

 

In your .m file

	int x = 0;
	for (int KeyId = 0; KeyId < 10; KeyId++)
	{
		UIButton *MyButton = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, MY_SCROLL_BUTTON_WIDTH, 100)];	//x, y, width, height
		//[MyButton setTitle:HarmonicKeyDataAll[KeyId] forState:UIControlStateNormal];
		
		UILabel *ButtonLabel1= [[UILabel alloc] initWithFrame:CGRectMake(0, 20, MY_SCROLL_BUTTON_WIDTH, 40)];	//x, y, width, height
		ButtonLabel1.font = [UIFont boldSystemFontOfSize:30.0];
		ButtonLabel1.text = MyKeyData[KeyId];
		ButtonLabel1.numberOfLines = 1;
		ButtonLabel1.textAlignment = NSTextAlignmentCenter;
		ButtonLabel1.textColor = [UIColor whiteColor];
		[MyButton addSubview:ButtonLabel1];
			
		UILabel *ButtonLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, MY_SCROLL_BUTTON_WIDTH, 30)];
		ButtonLabel2.font = [UIFont boldSystemFontOfSize:12.0];
		ButtonLabel2.text = MyKeyDataMusical[KeyId];
		ButtonLabel2.numberOfLines = 1;
		ButtonLabel2.textAlignment = NSTextAlignmentCenter;
		ButtonLabel2.textColor = [UIColor whiteColor];
		[MyButton addSubview:ButtonLabel2];
		
		[self.MyScrollView addSubview:MyButton];
		
		x += CurrentKeyButton.frame.size.width;
	}
	
	self.MyScrollView.contentSize = CGSizeMake(x, self.MyScrollView.frame.size.height);
	//self.MyScrollView.backgroundColor = [UIColor redColor];
	

In Your .xib

Link the ScrollView from the View Controller to the UIScrollView.

Then right click the 'View Controller' in the tree and in 'Referencing Outlets' select 'New Referencing Outlet', grag it onto the UIScrollView and then select 'delegate'

To Force It To Stop On A Button



//********************************************************************
//********** FORCE SCROLL BAR TO STOP ON A BUTTON WITHIN IT **********
//********************************************************************
- (void)scrollViewWillEndDragging:(UIScrollView *MyScrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
	//targetContentOffset = The expected offset when the scrolling action decelerates to a stop.
	
    // Determine which table cell the scrolling will stop on.
    NSInteger cellIndex = floor(targetContentOffset->x / MY_SCROLL_BUTTON_WIDTH);
	
    // Round to the next cell if the scrolling will stop over halfway to the next cell.
    if ((targetContentOffset->x - (floor(targetContentOffset->x / MY_SCROLL_BUTTON_WIDTH) * MY_SCROLL_BUTTON_WIDTH)) > (MY_SCROLL_BUTTON_WIDTH >> 1))
	{
        cellIndex++;
    }
	
    // Adjust stopping point to exact beginning of cell.
    targetContentOffset->x = cellIndex * MY_SCROLL_BUTTON_WIDTH;
}

To remove any existing sub views (i.e. to regenerate)


	//Remove any existing subviews
	for(UIView *subview in [self.MyScrollView subviews])
	{
		[subview removeFromSuperview];
	}

Scroll to position


	[self.MyScrollView setContentOffset:CGPointMake((NewKey * MY_SCROLL_BUTTON_WIDTH),0) animated:YES];

 

 

 

UK Electronic Designer