- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在我的应用程序中使用来自 iOS 11 的 prefersLargeTitles
。它在 UITableViewController
的子类中按预期工作:
navigationController?.navigationBar.prefersLargeTitles = true
但是,在一种情况下,我需要继承 UIViewController
并自己添加一个 TableView 。在这种情况下,我需要自己将表约束到 View :
tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.otherView.topAnchor).isActive = true
虽然这些约束按照我的预期显示了 tableView
,但现在导航栏始终使用大标题。我想模仿 UITableViewController
的行为,这样当 tableView
滚动到顶部时,会显示大标题,否则标题会折叠成普通标题。
如何解决?
最佳答案
我注意到 prefersLargeTitle
行为的另一个方面,在某些情况下甚至可以提供更简单、更优雅的解决方案。在我的例子中,viewController
不仅包含 tableView
(否则我会简单地使用 UITableViewController
并且我会得到标准的 prefersLargeTitle
behavior out-of-the-box),还有一些其他的 View 。现在我注意到,如果您将 tableView
添加为 viewController.view
的第一个 subview ,表格将控制大标题功能:
// this will work
fileprivate func setupInitialHierarchy() {
self.view.addSubview(tableView)
self.view.addSubview(logoffButton)
}
在我按如下方式创建 View 层次结构之前:
// for some reason now the tableView will not control the large title
fileprivate func setupInitialHierarchy() {
self.view.addSubview(logoffButton)
self.view.addSubview(tableView)
}
所以看起来如果 tableView
是 viewController
的 view
的第一个 subview ,我们就会得到标准的大标题行为。
替代方案
但是,如果这不可能,我已经能够通过这种方式以编程方式模拟标准行为:
为响应滚动的 tableView
实现委托(delegate)方法,然后运行使用当前 contentOffset
的代码来显示或隐藏大标题(UITableView
继承自 UIScrollView
,因此 scrollView
参数在这种情况下指的是 tableView
):
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
self.navigationItem.largeTitleDisplayMode = .always
} else {
self.navigationItem.largeTitleDisplayMode = .never
}
self.navigationController?.navigationBar.setNeedsLayout()
self.view.setNeedsLayout()
UIView.animate(withDuration: 0.25, animations: {
self.navigationController?.navigationBar.layoutIfNeeded()
self.view.layoutIfNeeded()
})
}
请记住 scrollViewDidScroll
会被重复调用,因此可能需要一些 guard
。
关于swift - 如何在 iOS 11 上模仿 `UITableViewController` 显示 `navigationBar` 中的大标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178893/
这个问题在这里已经有了答案: How to test your code on a machine with big-endian architecture? (3 个答案) 关闭 4 年前。 我想
我正在使用 Mockito 对业务对象进行单元测试。业务对象使用通常从数据库获取数据的 DAO。为了测试业务对象,我意识到使用单独的内存 DAO(将数据保存在 HashMap 中)比编写所有 when
如何实现: if X in (1,2,3) then 而不是: if x=1 or x=2 or x=3 then 换句话说,如何才能最好地模仿 Excel 的 VBA 中的 IN 运算符? 最佳答案
我正在使用带有 Jabber-net 的 ejabberd 2。我想问你是否可以在使用管理员登录时冒充用户。意思是我希望能够使用管理员帐户登录并发送消息 From: 'User A', To: 'Us
当查询输入到 Excel 中的 Bloomberg BDH 函数中时,它会自动填充选定的单元格,并使用请求的数据填充下面的单元格。 我正在尝试创建一个执行类似操作的函数(尽管使用不同的数据源)。 BD
在 es6 中保留模拟静态属性的类的属性的最佳方法是什么? 使用原型(prototype)链中创建的属性安全吗? class Employee { constructor(name, creato
我有一个 HTML 文本输入 用户输入字符串。由于它是文本输入而不是文本区域,因此用户无法输入换行符。 用户可以在文本输入中输入 Markdown 元素,以便在稍后重新显示数据时格式化数据。然而,他们
我有一个快速的问题:我正在使用 Spyder,变量浏览器选项卡中有一个巧妙的功能,我可以单击标题并对列进行排序。但是,当我尝试使用时无法重现相同的排序顺序: df.sort_values() 在 py
这是一个有趣的挑战。我正在阅读 TypeScript github 中的这个老问题 support Extension Methods用法与C#类似。提出了两种主要方法,一种是添加语法糖来扩展经常令人
在 Java 中这是有效的: class Class1 { T t; ... } //Inside other class that has no relation to Class1 private
我有另一个对象,我希望它的颜色与 UITextField 的默认占位符文本颜色相同。 我知道我可以简单地创建一个具有相同颜色的新 UIColor,但如果 Apple 更改默认的 UITextField
我正在尝试为我的 ListView 项目构建一个类似于 Gmail 应用程序标签列表的布局,其中标签文本在左侧,计数在右侧。除了长文本,我的大部分工作都有效。我所得到的结果导致文本与计数重叠。 这就是
我用一副纸牌创建了一个圆圈,用户可以旋转它来选择一张纸牌。平移结束后,它会捕捉到指定的角度,并带有漂亮的减速动画。将来会有某种指示表明 45 度的卡是所选的卡。我想指出选择随着触觉反馈而改变,就像在
我有一个简单的链表类型和一个 Clone 的实现它: #[deriving(Show)] enum List { Cons(int, Box), Nil, } impl Clone f
所以我创建了自己的自定义 UIView,它看起来像一个警报,现在我想添加显示和隐藏动画。 我想为 AlertController 模仿 Apple 的默认动画。解雇是一个简单的淡入淡出动画,但是我不确
我正在尝试让我的 Javascript 生活变得更轻松一些(至少对于我的工作环境而言)。我试图将所有 Javascript 对象创建方法移至“类”中。我有以下代码(用于创建超链接): function
您将如何模仿 iPhone 的键盘输入。因此,当您单击一个时,会显示 1,然后显示 2,然后显示 12... 依此类推,并显示 ( ) -。我不想使用实际的电话应用程序,因为我正在创建一个虚假的拨号器
你好,我有一个问题,我需要一些指导/帮助来创建一个菜单,如果屏幕太小而无法显示原始菜单,该菜单将替换为一个按钮。我知道 Bootstrap 会为您做这件事,但由于实现限制,我无法使用该库。因此,我查看
我正在使用 UIBarButtonItems 来触发特殊操作,但我还想在屏幕底部添加另一个按钮,其尺寸与在 NavigationBar 中创建的按钮的尺寸相同,我该怎么做? 我可以在 Interfac
我可以原谅可能重复的问题,但我没有找到解决问题的方法。 Controller 加载时模拟按钮点击。它运行良好,直到我将 ng-click 更改为 on-tap( ionic 应用程序)。现在根本不起作
我是一名优秀的程序员,十分优秀!