我有一个UICollectionView,其中包含UICollectionViewCell数组,每个单元格占用视图的80%。
我想在屏幕上添加一个静态UIButton,每次按下时滚动到下一个单元格,我将不得不在父视图中添加按钮subview而不是UICollectionView使其成为静态。
我的问题是:如何将叠加层添加到主视图上,以及如何通过按下按钮以编程方式滚动视图?
我实施集合视图的位置

@interface EvaluationViewController () <UICollectionViewDataSource,  UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cancelButton;
@property (nonatomic, strong) DBManager* dbManager;

@end


@implementation EvaluationViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"emokitDb.sqlite"];
NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
[self loadProjectWithId:1];


 }





   - (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

Screen * screen = [self.project.screens objectAtIndex:indexPath.row];

static NSString * cellIdentifier = @"EvaluationCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


return cell;
}
-(void)loadProjectWithId:(int)projectId {
}
@end

最佳答案

这很容易:

您可以像这样添加按钮:
而且您必须为按钮指定一个目标以移动collectionView contentOffset ...

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.dbManager = [[DBManager alloc]         initWithDatabaseFilename:@"emokitDb.sqlite"];
 NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
 [self loadProjectWithId:1];
 UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(910, 400 , 100, 100);
button.backgroundColor =[UIColor blackColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}

然后,在选择器中,您必须实现更改contentOffset的方法
 - (IBAction)changeContentOffset:(id)sender {
        [self.collectionView setContentOffset:CGPointMake(nextCellXValue, 0) animated:YES]
}

关于ios - 将静态UIButton覆盖到UICollectionView上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28495529/

10-15 15:20