如何在iOS中制作水平滚动菜单 [英] how to make horizontal scrolling menu in iOS
问题描述
我想制作一个水平滚动的菜单。
I would like to make a menu which will have horizontal scrolling.
该菜单共包含16个类别。所以我计划在第一部分拿8分,在另一部分拿8分。
The menu contains total 16 categories. So I am planning to take 8 on first part and rest 8 on another part.
有人可以让我了解需要做什么吗?
Can some-one give me insight of what needs to be done?
我相信我需要在下面使用。
I believe I need to use below.
UIScrollView
Add buttons in this scrollview
这就是它?
我想要的是第一个屏幕8个按钮,其中第一个屏幕将有两行,每行设置4个按钮。
What I want is on first screen 8 buttons where first screen will have two rows with 4 buttons set on each row.
推荐答案
如果您所做的只是向水平滚动视图添加按钮,您将执行以下操作...
If all you're doing is adding buttons to a horizontal scroll view you would do something like follows...
- (void)createScrollMenu
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
int x = 0;
for (int i = 0; i < 8; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
[button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
[scrollView addSubview:button];
x += button.frame.size.width;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
}
这将创建一个高度为100,宽度与其一样大的滚动视图父,并添加8个按钮。
This will create a scrollview with a height of 100, width as big as its parent, and add 8 buttons to it.
这篇关于如何在iOS中制作水平滚动菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!