如何在赢得';t在iOS中滚动表格
我想把按钮放在如何在赢得';t在iOS中滚动表格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想把按钮放在UITableView上,它保持在屏幕的相同位置,不会随着UITableView滚动。当我尝试addSubview按钮时,它们会显示出来,但会使用UITableView滚动 在UITableView上放置按钮的原因是为了让用户对其进行操作。我不是说牢房里有按钮。它们是浮动按钮,从不移动其位置 我正在使用UITableViewController,我不想为此使用页眉或页脚。另外,另一个some bar(例如,UIToolbar)不是我的选择 提前感谢。解决方法之一是扩展UIViewC
UITableView
上,它保持在屏幕的相同位置,不会随着UITableView
滚动。当我尝试addSubview
按钮时,它们会显示出来,但会使用UITableView
滚动
在UITableView上放置按钮的原因是为了让用户对其进行操作。我不是说牢房里有按钮。它们是浮动按钮,从不移动其位置
我正在使用UITableViewController
,我不想为此使用页眉或页脚。另外,另一个some bar(例如,UIToolbar
)不是我的选择
提前感谢。解决方法之一是扩展
UIViewController
,而不是UITableViewController
。您需要添加自己的表视图并设置所有内容。然后,您可以将按钮添加到视图控制器的视图,而不是表视图。您也可以使用UITableViewController
(不需要普通的UIViewController
,它嵌套您的表或VC)
使用安全区域更新iOS11+
您希望使用“自动布局”将视图保持在底部
{
[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];
}
- (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];
}
如果要浮动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];
}
- (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];
您可以在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];
我刚刚看了WWDC 2011桌面视图、提示和技巧视频。 浮动视图是完全相同的。您所要做的就是将帧更改回scrollViewDidScroll上的原始位置 查看视频表格查看提示和技巧。有关swift:
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不断移动,因此对用户来说,它看起来像是浮动的。如果要显示覆盖表视图的静态控件视图,有三个选项: 1) 不要使用UITableViewController。而是使用UITableView子视图创建UIViewController,并将表视图委托/数据源代码放入UIViewController中。此选项可能有效,也可能无效,具体取决于应用程序的体系结构。例如,如果您试图用大量自定义逻辑实现一个子类UITableViewController,这并不理想 2) WWDC 2011方法。请参阅WWDC 2011课程125“UITableView更改、提示和技巧”,从32:20开始。在iOS引入AutoLayout之前,以及在我们需要处理如此多不同的屏幕大小之前,Apple推荐了这种在表格视图滚动时调整视图位置的方法。我不推荐这样做,因为你将自己管理所有不同屏幕大小的位置 3) 我建议将UIViewController与包含UITableViewController的容器视图一起使用。通过这种方式,您可以在UITableViewController中保持表视图逻辑的独立性。UIViewController为UITableViewController托管一个“哑”容器,它还保存静态控件视图。要执行此操作,请将UIViewController拖动到情节提要,并在其中拖动“容器视图”。删除自动附加到容器视图的UIViewController,然后通过将控件从contrainer视图拖动到表视图控制器并选择“嵌入”来附加要在容器视图中显示的表视图控制器。现在,您可以将静态控件作为子视图添加到将显示在表视图顶部的UIViewController中。它看起来像这样:
我更喜欢这种方法,因为UIViewController可以处理控制逻辑,而UITableViewController可以处理表视图逻辑。这里的一些其他答案建议将静态控件作为子视图添加到导航控制器或窗口。只要您从未将其他视图控制器添加到导航控制器或窗口,这些功能就可以正常工作。我创建了一个库,以便更容易在UITableView/UICollectionView/UIScrollView顶部添加浮动按钮。被称为 以下是使用它的步骤 1-导入类别文件
#导入“UIScrollView+FloatingButton.h”
2-添加委托和可选委托方法。
@interface ViewController () <MEVFloatingButtonDelegate>
#pragma mark - MEScrollToTopDelegate Methods
- (void)floatingButton:(UIScrollView *)scrollView didTapButton:(UIButton *)button;
4-将按钮和委托分配给UITableView。
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;
[self.tableView setFloatingButtonView:button];
[self.tableView setFloatingButtonDelegate:self]
演示结果
有一个更好的解决方案。可以通过禁用相应的
按钮
或任何ui视图
对于浮动按钮:
斯威夫特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
}
我希望我的类是一个
UITableViewController
子类,子视图需要是一个常规的子视图,使用自动布局(如果我愿意)。下面是我所做的:
private弱var\u tableView:UITableView?
覆盖变量tableView:UITableView!{
得到{
返回自我。\u tableView
}
设置{
self.\u tableView=newValue
}
}
重写func viewDidLoad(){
//重新生根
让tableView=super.tableView!//保留引用