将静态UIButton叠加到UICollectionView上

时间:2015-02-13 08:42:32

标签: ios objective-c uibutton uicollectionview overlay

我有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

1 个答案:

答案 0 :(得分:1)

这很容易:

你可以像这样添加按钮: 而且你必须给按钮一个目标来移动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]
}