【发布时间】:2016-05-28 09:06:10
【问题描述】:
对于我的 iOS 应用程序 (swift),我正在尝试在 tableView 上方添加一个自定义 UIView。当用户拉下 tableView 时,应用程序应如此屏幕截图所示。
顶部蓝色区域中的文本(“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标题和刷新控制视图之间的小间隙是可见的(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
【问题讨论】:
标签: ios swift uitableview uiview refresh