Ios 将静态UIButton覆盖到UICollectionView上

Ios 将静态UIButton覆盖到UICollectionView上,ios,objective-c,uibutton,uicollectionview,overlay,Ios,Objective C,Uibutton,Uicollectionview,Overlay,我有一个UICollectionView,它有一个UICollectionViewCell数组,每个单元格占据了视图的80%。 我想在屏幕上添加一个静态的ui按钮,每次按下按钮都会滚动到下一个单元格,我必须将按钮子视图添加到父视图,而不是ui集合视图,才能使其成为静态的。 我的问题是:如何将覆盖添加到主视图上,以及如何通过按按钮以编程方式滚动视图? 在那里我实现了我的集合视图 @interface EvaluationViewController () <UICollectionViewD

我有一个
UICollectionView
,它有一个
UICollectionViewCell
数组,每个单元格占据了视图的80%。 我想在屏幕上添加一个静态的
ui按钮
,每次按下按钮都会滚动到下一个单元格,我必须将按钮
子视图
添加到父视图,而不是
ui集合视图
,才能使其成为静态的。 我的问题是:如何将覆盖添加到主视图上,以及如何通过按按钮以编程方式滚动视图? 在那里我实现了我的集合视图

@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
@接口EvaluationViewController()
@属性(弱,非原子)IBUIBarbuttonItem*cancelButton;
@属性(非原子,强)DBManager*DBManager;
@结束
@实现评估视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
self.dbManager=[[dbManager alloc]initWithDatabaseFilename:@“emokitDb.sqlite”];
NSLog(@“self.dbManager%@”,self.dbManager.documentsDirectory);
[ID为1的自加载项目];
}
-(iAction)取消按钮:(id)发送方{
[自我解除视图控制器激活:是完成:无];
}
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回4;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
Screen*Screen=[self.project.screens objectAtIndex:indexPath.row];
静态NSString*cellIdentifier=@“EvaluationCell”;
UICollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier for indexPath:indexPath];
返回单元;
}
-(void)loadProjectWithId:(int)projectId{
}
@结束
这很简单:

您可以这样添加按钮: 您必须为按钮指定一个目标以移动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]
}

为按钮指定框架,将其添加到主视图,单击按钮时,调整collectionView的contentOffset。@LenvanZyl,您可以在将集合视图添加到主视图的任何位置添加这些方法。如果你用你的代码编辑你的帖子,我可以在里面写…@LenvanZyl否,你可以在控制器的ViewDidLoad中添加它。。。首先,您必须为按钮提供一个框架(我将在我的回答中编辑它…现在唯一的问题是UIButton*按钮=[[UIButton alloc]initWithFrame:CGRectMake:(10,10,9,100)];给我一个错误。UIButton没有可见的@interface声明选择器initWithFrame