ios 如何在iOS中制作水平滚动菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18069007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:37:38  来源:igfitidea点击:

how to make horizontal scrolling menu in iOS

iosobjective-cmenuhorizontalscrollview

提问by Fahim Parkar

I would like to make a menu which will have horizontal scrolling.

我想做一个水平滚动的菜单。

The menu contains total 16 categories. So I am planning to take 8 on first part and rest 8 on another part.

菜单共包含 16 个类别。所以我打算在第一部分拿 8 分,在另一部分拿 8 分。

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

That's it?

就是这样?



What I want is on first screen 8 buttons where first screen will have two rows with 4 buttons set on each row.

我想要的是第一个屏幕 8 个按钮,其中第一个屏幕将有两行,每行设置 4 个按钮。

Menu sample can be seen at http://www.shoutem.com/

菜单示例可以在http://www.shoutem.com/上看到

回答by Eric

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];
    }

This will create a scrollview with a height of 100, width as big as its parent, and add 8 buttons to it.

这将创建一个高度为 100、宽度与其父级一样大的滚动视图,并为其添加 8 个按钮。

回答by Matt

You can accomplish you're goal using a UIScrollView and your UIButton objects, it would involve setting each button's frame / layout properties depending on what iOS version you're targeting. (As in Eric's answer).

您可以使用 UIScrollView 和您的 UIButton 对象来实现您的目标,这将涉及根据您的目标 iOS 版本设置每个按钮的框架/布局属性。(如埃里克的回答)。

However, if you're targeting iOS 6 and above, using a UICollectionView where your items/cells are the buttons, then you can get the horizontal scrolling "menu bar" for free. There are plenty of SO posts on this, but the main idea is to use a flow layout where the item size has a height such that there will only be one row of items (just make the item height the same as the collection view's height).

但是,如果您的目标是 iOS 6 及更高版本,使用 UICollectionView,其中您的项目/单元格是按钮,那么您可以免费获得水平滚动的“菜单栏”。有很多关于此的 SO 帖子,但主要思想是使用流布局,其中项目大小具有高度,以便只有一行项目(只需使项目高度与集合视图的高度相同) .

EDIT:

编辑:

I should say, this might seem like overkill (and maybe it is), but you will end up with a much more flexible component in case requirements change in the future. It also doesn't result in much extra code and abstracts away tedious layout details.

我应该说,这可能看起来有点矫枉过正(也许确实如此),但是如果将来需求发生变化,您最终会得到一个更加灵活的组件。它也不会产生太多额外的代码并抽象出繁琐的布局细节。