Objective c UITableview上带有图像的浮动UIButton
我尝试在Objective c UITableview上带有图像的浮动UIButton,objective-c,uitableview,uibutton,overlay,floating,Objective C,Uitableview,Uibutton,Overlay,Floating,我尝试在UITableview上实现一个浮动按钮。这样,当内容滚动时,按钮保持在相同的位置。按钮应保持透明图像 我做了一个测试,它分别与insertSubview:oversubview:和addSubView 但在TableViewController的UITableview上执行此操作似乎不起作用。该按钮位于tableView上,并与tableView一起滚动。有趣的是,它也会在tableView的标题下滚动 我如何解决这个问题-按钮在tableview上浮动 TableViewContro
UITableview
上实现一个浮动按钮。这样,当内容滚动时,按钮保持在相同的位置。按钮应保持透明图像
我做了一个测试,它分别与insertSubview:oversubview:
和addSubView
但在TableViewController
的UITableview
上执行此操作似乎不起作用。该按钮位于tableView上,并与tableView一起滚动。有趣的是,它也会在tableView的标题下滚动
我如何解决这个问题-按钮在tableview上浮动
TableViewController.m(使用工作代码12.07.2013更新)
编辑:添加了一个@属性(非原子)float originalogin
-(void)viewDidLoad
{
[超级视图下载];
自身原发基因=424.0;
[自添加叠加按钮];
}
#pragma标记摄影机按钮
-(无效)添加覆盖按钮{
//UIButton*oButton=[UIButton Button类型为:uiButtonTypeLoundRect];
self.oButton=[UIButton buttonWithType:UIButtonTypeCustom];
self.oButton.frame=CGRectMake(132.0,self.originalOrigin,56.0,56.0);
self.oButton.backgroundColor=[UIColor clearColor];
[self.oButton setImage:[UIImage ImageName:@“CameraButton56x56.png”]用于状态:UIControlStateNormal];
[self.oButton addTarget:self action:@selector(toCameraView:)for controlEvents:UIControlEventTouchDown];
[self.view insertSubview:self.oButton subview:self.view];
}
//使按钮浮动在包括tableHeader的tableView上
-(无效)scrollViewDidScroll:(UIScrollView*)scrollView{
CGRect tableBounds=self.tableView.bounds;
CGRect floatingButtonFrame=self.oButton.frame;
floatingButtonFrame.origin.y=self.originalOrigin+tableBounds.origin.y;
self.oButton.frame=浮动按钮框架;
[self.view带来subviewTofront:self.oButton];//浮动在表格标题上
}
您可以执行scrollViewDidScroll
委托,在用户滚动表格时更改浮动按钮y原点,以便按钮浮动并保持在同一位置
在WWDC 2011年第125期会议中有一个很好的例子,它正是你想要的
通常,创建按钮后需要保存按钮的原始y原点,或者可以在viewDidLoad
中执行,然后执行scrollViewDidScroll
并更新按钮框
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds;
CGRect floatingButtonFrame = self.floatingButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.floatingButton.frame = floatingButtonFrame;
}
你的解决方案部分有效。按钮现在停留在它应该停留的地方。但我有一个奇怪的行为,按钮在tablecells上,但在tableheader下。因此,每当tableheader出现时,按钮就会在tableheader下滚动。找到解决方案,添加:[self.view bringSubviewToFront:self.oButton]代码>到scrollViewDidScroll
是否使用工作解决方案更新了我的代码-(void)scrollViewDidScroll:(UIScrollView*)scrollView{if(scrollView==\u tableContacts){CGRect frame=btnAddNewContact.frame;frame.origin.y=scrollView.contentOffset.y+tableContacts.frame.size.height-btnAddNewContact.frame.size.height;btnAddNewContact.frame=frame;[self.view带来子视图前面:btnAddNewContact];}最佳答案