您可以尝试使用UICollectionView
withUICollectionViewFlowLayout
创建一个可用于在不同主题之间切换的按钮网格。
如果不了解更多关于您想要从选择菜单中获得什么、您有多少主题、您希望它如何显示等信息,那么很难推荐一种方法而不是另一种方法。但由于是Apple 提供UICollectionViewFlowLayout
的预定义以网格排列方式显示,因此它可能是解决此问题的好方法。UICollectionViewLayout
UIViews
以下是您将如何实现UICollectionView
with UICollectioViewFlowLayout
:
头文件
@interface ViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
UICollectionView * themeSelection;
}
实施文件
- (void)viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
themeSelection =[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[themeSelection setDataSource:self];
[themeSelection setDelegate:self];
[themeSelection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.view addSubview: themeSelection];
[super viewDidLoad];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
//Each cell is a theme that could be selected
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 100);
}