【问题标题】:Create a floating button over a UITableView [duplicate]在 UITableView 上创建一个浮动按钮 [重复]
【发布时间】:2014-03-10 13:18:55
【问题描述】:

我看到iOS floating button over table view 关于如何创建浮动按钮。这就是我创建按钮的方式。问题是按钮会随着表格视图滚动。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button]; 

我该如何解决这个问题?

我建议你使用https://github.com/gizmoboy7/VCFloatingActionButton这个链接....希望它对你有用

【问题讨论】:

标签: ios objective-c uitableview cocoa-touch layout


【解决方案1】:

将您的视图控制器指定为表格视图的委托并覆盖scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]) {
        self.floatingButton.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);     
    }
}

【讨论】:

  • 感谢 Austin,我如何将视图控制器委托给表视图。谢谢
  • 您可以将self.tableView.delegate = self 添加到viewDidLoad,或者在Interface Builder 中连接代理出口。
【解决方案2】:

试试这个:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint origin = [scrollView contentOffset];
    [scrollView setContentOffset:CGPointMake(0.0, origin.y)];

    if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0)
    {
        NSLog(@"Now TableView Scrolling UP");
        // Your Custom Code
    } 
    else 
    {
        NSLog(@"Now TableView Scrolling Down");
        // Your Custom Code
    }
}

【讨论】:

  • 希望对你有所帮助..
猜你喜欢
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多