我想把按钮放在UITableView
屏幕的同一位置,不会滚动UITableView
。当我尝试addSubview
按钮时,它们会显示但会滚动UITableView
。
放置按钮的原因UITableView
是让用户对其进行一些操作。我不是在谈论在单元格中有按钮。它们是永远不会移动位置的浮动按钮。
我正在使用UITableViewController
,我不想为此使用页眉或页脚。还有另一个酒吧(UIToolbar
例如)对我来说不是一个选择。
提前致谢。
我想把按钮放在UITableView
屏幕的同一位置,不会滚动UITableView
。当我尝试addSubview
按钮时,它们会显示但会滚动UITableView
。
放置按钮的原因UITableView
是让用户对其进行一些操作。我不是在谈论在单元格中有按钮。它们是永远不会移动位置的浮动按钮。
我正在使用UITableViewController
,我不想为此使用页眉或页脚。还有另一个酒吧(UIToolbar
例如)对我来说不是一个选择。
提前致谢。
你也可以这样做UITableViewController
(不需要普通的UIViewController
,它嵌套你的表或VC)
您想使用自动布局将视图保持在底部
{
[self.view addSubview:self.bottomView];
[self.bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];
UILayoutGuide * safe = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[[safe.trailingAnchor constraintEqualToAnchor:self.bottomView.trailingAnchor],
[safe.bottomAnchor constraintEqualToAnchor:self.bottomView.bottomAnchor],
[safe.leadingAnchor constraintEqualToAnchor:self.bottomView.leadingAnchor]]];
[self.view layoutIfNeeded];
}
并确保视图始终位于顶部
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view bringSubviewToFront:self.bottomView];
}
通过滚动表格,您需要调整“浮动”视图的位置,就可以了
如果您在 UITableViewController 中,只需
如果你想在 UITableView 的顶部浮动视图
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect frame = self.floatingView.frame;
frame.origin.y = scrollView.contentOffset.y;
self.floatingView.frame = frame;
[self.view bringSubviewToFront:self.floatingView];
}
如果你想浮动 UITableView 底部的视图
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect frame = self.floatingView.frame;
frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.floatingView.frame.size.height;
self.floatingView.frame = frame;
[self.view bringSubviewToFront:self.floatingView];
}
我相信这就是你问题的真正答案
如果您使用的是导航控制器,您可以将视图添加到其中:
[self.navigationController.view addSubview:yourView];
例子:
UIButton *goToTopButton = [UIButton buttonWithType:UIButtonTypeCustom];
goToTopButton.frame = CGRectMake(130, 70, 60, 20);
[goToTopButton setTitle:@"Scroll to top" forState:UIControlStateNormal];
[goToTopButton addTarget:self action:@selector(goToTop) forControlEvents:UIControlEventTouchUpInside];
[goToTopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[goToTopButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
goToTopButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
[self.navigationController.view goToTopButton];
如果你想显示一个覆盖表格视图的静态控件视图,你有三个选项:
1) 不要使用 UITableViewController。而是创建一个带有 UITableView 子视图的 UIViewController 并将您的 Table View 委托/数据源代码放在 UIViewController 中。此选项可能会或可能不会起作用,具体取决于您的应用程序的架构。例如,如果您尝试使用大量自定义逻辑实现 UITableViewController 的子类,这并不理想。
2) WWDC 2011 方法。请参阅 WWDC 2011 Session 125 “UITableView Changes, Tips & Tricks”,从 32:20 开始。这种在表格视图滚动时调整视图位置的方法是在 AutoLayout 被引入 iOS 之前以及在我们有这么多不同的屏幕尺寸需要处理之前,Apple 推荐的。我不推荐它,因为您将自己管理所有不同屏幕尺寸的位置。
3)我建议使用 UIViewController 和包含 UITableViewController 的容器视图。 这样,您可以在 UITableViewController 中保持表格视图逻辑分开。UIViewController 为 UITableViewController 托管一个“哑”容器,它还包含静态控件视图。为此,将 UIViewController 拖到情节提要并在其中拖入“容器视图”。删除自动附加到容器视图的 UIViewController,然后通过将控件从约束视图拖到表视图控制器并选择“嵌入”来附加要在容器视图中显示的表视图控制器。现在,您可以将静态控件作为子视图添加到 UIViewController,它将显示在表格视图的顶部。它看起来像这样:
我更喜欢这种方法,因为 UIViewController 可以处理控制逻辑,而 UITableViewController 处理表格视图逻辑。这里的其他一些答案建议将静态控件作为子视图添加到导航控制器或窗口。这些仅在您从未将另一个视图控制器添加到导航控制器或窗口时才有效。
对此的一种解决方案是扩展UIViewController
而不是UITableViewController
. 您将需要添加自己的表格视图并设置所有内容。然后您可以将按钮添加到视图控制器的视图而不是表格视图。
对于迅速:
override func scrollViewDidScroll(scrollView: UIScrollView){
var frame: CGRect = self.floatingView.frame
frame.origin.y = scrollView.contentOffset.y
floatingView.frame = frame
view.bringSubviewToFront(floatingView)
}
每次滚动时都会调用此函数。然后在里面你得到你的 UIView 的框架并根据你滚动的程度更新它的位置。您的 UIView 会随着您的 UIScrollView 不断移动,因此对用户来说它看起来像是浮动的。
您可以在 UIWindow 上添加按钮,无需将其扩展到 UIViewController 而不是 UITableviewController。只需在窗口上添加按钮。
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(60, 200,40, 60)];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:btn];
我创建了一个库,以便更轻松地在UITableView / UICollectionView / UIScrollView顶部添加浮动按钮。称为MEVFloatingButton。
这些是使用它的步骤。
1-导入类别文件
#import "UIScrollView+FloatingButton.h"
2 - 添加委托和可选的委托方法。
@interface ViewController () <MEVFloatingButtonDelegate>
#pragma mark - MEScrollToTopDelegate Methods
- (void)floatingButton:(UIScrollView *)scrollView didTapButton:(UIButton *)button;
2 - 创建一个 MEVFloatingButton 对象。
MEVFloatingButton *button = [[MEVFloatingButton alloc] init];
button.animationType = MEVFloatingButtonAnimationFromBottom;
button.displayMode = MEVFloatingButtonDisplayModeWhenScrolling;
button.position = MEVFloatingButtonPositionBottomCenter;
button.image = [UIImage imageNamed:@"Icon0"];
button.imageColor = [UIColor groupTableViewBackgroundColor];
button.backgroundColor = [UIColor darkGrayColor];
button.outlineColor = [UIColor darkGrayColor];
button.outlineWidth = 0.0f;
button.imagePadding = 20.0f;
button.horizontalOffset = 20.0f;
button.verticalOffset = -30.0f;
button.rounded = YES;
button.hideWhenScrollToTop = YES;
4 - 将按钮和委托分配给 UITableView。
[self.tableView setFloatingButtonView:button];
[self.tableView setFloatingButtonDelegate:self]
演示结果
有一个更好的解决方案。您可以通过禁用相应Button
或任何UIView
浮动按钮的 Auto Layout(button.translatesAutoresizingMaskIntoConstraints = false) 属性来执行此操作:
斯威夫特 4
//create a button or any UIView and add to subview
let button=UIButton.init(type: .system)
button.setTitle("NEXT", for: .normal)
button.frame.size = CGSize(width: 100, height: 50)
self.view.addSubview(button)
//set constrains
button.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
button.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
button.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
button.rightAnchor.constraint(equalTo: tableView.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
button.bottomAnchor.constraint(equalTo: tableView.layoutMarginsGuide.bottomAnchor, constant: -10).isActive = true
}
我刚刚看了 WWDC 2011 TableView、Tips and Tricks 视频。浮动视图完全一样。您所要做的就是将框架更改回scrollViewDidScroll上的原始位置。
https://developer.apple.com/videos/wwdc/2011/
查看视频 TableViews 提示和技巧。
我希望我的类成为UITableViewController
子类,并且子视图需要是常规子视图,使用自动布局(如果我愿意)。
所以,这就是我所做的:
private weak var _tableView:UITableView?
override var tableView: UITableView!{
get {
return self._tableView
}
set {
self._tableView = newValue
}
}
override func viewDidLoad() {
// Re-root
let tableView = super.tableView! // keep a reference to the original tableview
self.tableView = tableView
view = UIView() // Create my own view
tableView.translatesAutoresizingMaskIntoConstraints = true
view.addSubview(tableView)
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
您现在可以将任何您想要的内容添加到view
视图控制器,而tableView
原始表格视图是。
我知道问题出在 Objective C 中,但代码相当简单,我只是从自己的代码中复制粘贴。