【问题标题】:floating UIButton with image on a UITableview [duplicate]在 UITableview 上浮动带有图像的 UIButton [重复]
【发布时间】:2013-07-11 12:41:55
【问题描述】:

我尝试在UITableview 上实现一个浮动按钮。这样当内容滚动时,按钮保持在同一位置。按钮应保持透明图像。

我做了一个smoketest,它与insertSubview:aboveSubview:respectively addSubView 配合得很好

但是在UITableviewTableViewController 上执行此操作似乎不起作用。 Button 位于 tableView 上并随 tableView 滚动。有趣的是,它也会在 tableView 的标题下滚动...

如何解决这个问题 - 按钮浮动在 tableview 上?

TableViewController.m(使用工作代码 12.07.2013 更新)

编辑:在 TableViewController.h 中添加了 @property (nonatomic) float originalOrigin;

- (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
}

【问题讨论】:

标签: objective-c uitableview uibutton overlay floating


【解决方案1】:

您可以实现scrollViewDidScroll 委托以在用户滚动表格时更改浮动按钮的 y 原点,以便按钮浮动并停留在同一位置。

WWDC 2011 session 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;
}

【讨论】:

  • 您的解决方案部分有效。该按钮现在停留在应有的位置。但我有奇怪的行为,按钮在表格单元上方,但在表格标题下方。所以每当表格标题出现时,按钮就会在表格标题下方滚动..
  • 找到了解决方案,将:[self.view bringSubviewToFront:self.oButton]; 添加到 scrollViewDidScroll 就可以了
  • 用有效的解决方案更新了我的代码
  • - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == _tableContacts) { CGRect frame = _btnAddNewContact.frame; frame.origin.y = scrollView.contentOffset.y + _tableContacts.frame.size.height - _btnAddNewContact.frame.size.height; _btnAddNewContact.frame = 框架; [self.view bringSubviewToFront:_btnAddNewContact]; } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2016-04-26
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多