000
000

Reputation: 225

Overlay a static UIButton onto a UICollectionView

I have a UICollectionView that has an array of UICollectionViewCell each cell takes up 80% of the view. I want to add a static UIButton onto the screen that scrolls to the next cell every time it is pressed, I will have to add the button subview onto the Parent view and not the UICollectionView for it to be static. My questions are : how do I add the overlay onto the main view and how do I scroll the view programmatically with the press of the button? Where I implemented my collection view

@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

Upvotes: 1

Views: 1420

Answers (1)

peig
peig

Reputation: 418

It is quite easy:

You can add the button like that: And you have to give a target to the button to move 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];
}

Then in the selector you have to implement a method to change contentOffset

 - (IBAction)changeContentOffset:(id)sender {
        [self.collectionView setContentOffset:CGPointMake(nextCellXValue, 0) animated:YES]
}

Upvotes: 1

Related Questions