浮动的UIButton,在UITableview上有图像 [英] floating UIButton with image on a UITableview
问题描述
我尝试在UITableview
上实现浮动按钮.这样,当内容滚动时,按钮将停留在相同的位置.该按钮应保持透明图像.
I try to implement a floating button over a UITableview
. So that when the content scrolls the button stay at the same position. The button shall hold a transparent image.
我做了一个 Smoketest ,它与insertSubview:aboveSubview:
分别addSubView
I did a smoketest and it worked fine with insertSubview:aboveSubview:
respectivly addSubView
但是在TableViewController
的UITableview
上执行此操作似乎无效. Button在tableView上,并与tableView一起滚动.有趣的是,它也在tableView的标题下滚动.
But doing this on a UITableview
of an TableViewController
does not seem to work. The Button is on the tableView and scrolls with the tableView. Interestingly it scrolls as well under the header of the tableView...
我该如何解决-按钮在表格视图上方浮动?
How do I fix this - that the button is floating over the tableview?
编辑:在TableViewController中添加了@property (nonatomic) float originalOrigin;
.h
Edit: Added a @property (nonatomic) float originalOrigin;
in TableViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
self.originalOrigin = 424.0;
[self addOverlayButton];
}
#pragma mark Camera Button
- (void)addOverlayButton {
// UIButton *oButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
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 imageNamed:@"CameraButton56x56.png"] forState:UIControlStateNormal];
[self.oButton addTarget:self action:@selector(toCameraView:) forControlEvents:UIControlEventTouchDown];
[self.view insertSubview:self.oButton aboveSubview:self.view];
}
// to make the button float over the tableView including tableHeader
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds;
CGRect floatingButtonFrame = self.oButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.oButton.frame = floatingButtonFrame;
[self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
推荐答案
您可以实现scrollViewDidScroll
委托,以在用户滚动表时更改浮动按钮y的原点,以便按钮将浮动并保持不变地点.
You can implement the scrollViewDidScroll
delegate to change your floating button y origin while the user scroll the table, so that the button will float and stay at the same location.
WWDC 2011会话125 中有一个很好的例子,它确实可以满足您的需求.
There is a great example in WWDC 2011 session 125 that does exactly what you want.
通常,创建按钮后需要保存按钮的原始y原点,或者可以在viewDidLoad
中进行操作,然后实现scrollViewDidScroll
并更新按钮框架.
In general, you need to save the original y origin of the button after you create it, or you can do it in viewDidLoad
, then implement the scrollViewDidScroll
and update the button frame.
- (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;
}
这篇关于浮动的UIButton,在UITableview上有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!