gpt4 book ai didi

ios - UITableView 上方的自定义 UIView(当用户拉动刷新时)

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:08 24 4
gpt4 key购买 nike

对于我的 iOS 应用程序 (swift),我正在尝试在 tableView 上方添加一个自定义 UIView。当用户下拉 tableView 时,应用程序应如该屏幕截图所示。

Screenshot of current status

顶部蓝色区域中的文本(“Zorneding ...”)仍然是 tableView 标题的一部分。文本上方的蓝色区域是一个 UIView,我已将其添加到 TableViewController 的 ViewDidAppear 函数中。

我用于在 ViewDidAppear 中添加 UIView 的代码如下。

    // Blue background view behind refresh control
var frame = tableView.bounds
frame.origin.y = -frame.size.height
frame.size.width -= 10
frame.origin.x += 5
let refreshBackgroundView = UIView(frame: frame)
refreshBackgroundView.backgroundColor = GlobalConstants.Color.ZeroBlue
tableView.insertSubview(refreshBackgroundView, atIndex: 0)

此代码在纵向模式下运行良好。但是,当将设备旋转到横向时,refreshBackgroundView 当然不会改变它的大小,因此只会覆盖部分屏幕。

我已经尝试将约束添加到 refreshBackgroundView,但我无法让它们工作。

我尝试过的另一件事是子类化 UIRefreshControl 并在那里配置它的背景,但是当下拉 tableView 时,tableView 标题和刷新控制 View 之间的小间隙是可见的(tableView 背景闪耀)。

知道如何在设备旋转时让我的代码正常工作吗?

谢谢!

解决方法:

解决方案非常简单。使用布局约束。我不知道为什么那些昨天对我不起作用。

这是新代码:

    // Add background view behind UIRefreshControl
let refreshBackgroundView = UIView()
refreshBackgroundView.backgroundColor = GlobalConstants.Color.ZeroBlue
tableView.insertSubview(refreshBackgroundView, atIndex: 0)

refreshBackgroundView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint(item: refreshBackgroundView, attribute: .Height, relatedBy: .Equal, toItem: tableView, attribute: .Height, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: refreshBackgroundView, attribute: .Width, relatedBy: .Equal, toItem: tableView, attribute: .Width, multiplier: 1.0, constant: -10.0).active = true
NSLayoutConstraint(item: refreshBackgroundView, attribute: .Bottom, relatedBy: .Equal, toItem: tableView, attribute: .Top, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: refreshBackgroundView, attribute: .CenterX , relatedBy: .Equal, toItem: tableView, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true

最佳答案

我会在 Xib 中设置 refreshBackgroundView 以及其余的界面元素。

就个人而言,我发现 Xibs 是设置布局约束的最佳/最直接的方法。如果您采用这种方法,则意味着您的约束应该在横向或纵向上都能正常工作,没有任何附加条件。

关于ios - UITableView 上方的自定义 UIView(当用户拉动刷新时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443522/

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