固定在 UITableview 底部的浮动按钮,scrollViewDidScroll 无法正常工作 [英] A floating button fixed at the bottom of UITableview with scrollViewDidScroll not working properly
本文介绍了固定在 UITableview 底部的浮动按钮,scrollViewDidScroll 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这段代码使 UIButton 浮动固定在UITable 的底部,但最初 UIButton 将准确地追加到下方UITableView 的最后一个单元格,只有当我滚动页面时 UIButton 才会到达底部,我该怎么做才能让按钮首先到达底部?提前致谢!
I am using this piece of code to make a UIButton floating fixed at bottom of the UITable, but initially the UIButton will append exactly below the last cell of the UITableView, only when I scroll the page the UIButton will get to the bottom, what can I do to make the button come to the bottom at first? Thanks in advance!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect frame = self.chatButtonView.frame;
frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.chatButtonView.frame.size.height;
self.chatButtonView.frame = frame;
[self.view bringSubviewToFront:self.chatButtonView];
}
推荐答案
这里,我在 tableview 中添加了浮动按钮,与添加任何其他控件的方式相同.
Here, i have added floating button in tableview, same way you can add any other control.
// Create button in `ViewDidLoad` and add to tableview.
- (void)viewDidLoad
{
[super viewDidLoad];
// *** Creat a button, calculate Y position at bottom of table, assign frame & add to tableview ***
_btnFloating = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat yPos = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
[_btnFloating setFrame:CGRectMake(0, yPos, [UIScreen mainScreen].bounds.size.width, 50)];
[self.tableView addSubview:_btnFloating];
// *** Adjust Inset and scroll indicator of tableview ***
self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
// *** Add observer on tableview frame change to update floating button frame ***
[self.tableView addObserver:self
forKeyPath:@"frame"
options:0
context:NULL];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if([keyPath isEqualToString:@"frame"]) {
[self adjustFloatingViewFrame];
}
}
- (void)adjustFloatingViewFrame
{
CGRect newFrame = _btnFloating.frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
_btnFloating.frame = newFrame;
[self.tableView bringSubviewToFront:_btnFloating];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self adjustFloatingViewFrame];
}
// Don't forget to remove Observer on Tableview otherwise it will lead to crash.
-(void)dealloc
{
[self.tableView removeObserver:self forKeyPath:@"frame"];
}
这篇关于固定在 UITableview 底部的浮动按钮,scrollViewDidScroll 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文