id | title | brief | article | sdk | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
C956274D-BD3A-D353-26EA-1C30097BF44A |
Create a Horizontal Scrolling Button List |
This recipe shows how to create a horizontal scrolling list of buttons. |
|
|
Recipe
Follow these steps to create a scrolling list of buttons in a
UIViewController
subclass:
- Declare
UIScrollView
andList<UIButton>
class variables.
UIScrollView scrollView;
List<UIButton> buttons;
- Instantiate the button list
List<UIButton>
in the constructor.
public ScrollingButtonsController ()
{
buttons = new List<UIButton> ();
}
- Initialize parameters for laying out the buttons.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
nfloat h = 50.0f;
nfloat w = 50.0f;
nfloat padding = 10.0f;
nint n = 25;
…
}
- Create the
UIScrollView
scrollView = new UIScrollView {
Frame = new CGRect (0, 100, View.Frame.Width, h + 2 * padding),
ContentSize = new CGSize ((w + padding) * n, h),
BackgroundColor = UIColor.White,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
- Create buttons and add them to the scroll view.
for (int i=0; i<n; i++) {
var button = UIButton.FromType (UIButtonType.RoundedRect);
button.SetTitle (i.ToString (), UIControlState.Normal);
button.Frame = new CGRect (padding * (i + 1) + (i * w), padding, w, h);
scrollView.AddSubview (button);
buttons.Add (button);
}
- Add the
UIScrollView
as a subview.
View.AddSubview (scrollView);
Additional Information
The UIScrollView
will scroll when the ContentSize
is greater than the size
defined by the UIScrollView.Frame
. Try experimenting with the layout parameters
in the step 3 to see the effect it has on the button list that is created.