gpt4 book ai didi

ios - 固定在 UITableview 底部的 float 按钮,scrollViewDidScroll 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 00:43:50 24 4
gpt4 key购买 nike

我正在使用这段代码使 UIButton float 固定在UITable 的底部,但最初 UIButton 将恰好附加在下方UITableView 的最后一个单元格,只有当我滚动页面时,UIButton 才会到底,我该怎么做才能让按钮首先到达底部?提前致谢!

  - (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 中添加了 float 按钮,您可以用同样的方式添加任何其他控件。

// 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"];
}

关于ios - 固定在 UITableview 底部的 float 按钮,scrollViewDidScroll 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38887389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com