Swift: 使Navigation Controller中嵌入的UITableViewController的背景图充满整个屏幕
Swift: Make the background image of UITableViewController embedded in Navigation Controller fill the entire screen
我设法在嵌入导航控制器的 UITableViewController 中创建半透明和圆形的 UITableViewCells,并在 viewDidLoad():
中使用这行代码
tableView.backgroundView = UIImageView(image: UIImage(named: "nightTokyo"))
但我希望背景图片填满整个 phone 屏幕。我将代码(和仅这行代码)更改为:
navigationController?.view = UIImageView(image: UIImage(named: "nightTokyo"))
现在背景图像填满了整个 phone 屏幕,但是我的 table 甚至 iPhone 的时间和电池指示器图标都不见了。
我想要的是背景图片填满整个屏幕,但 tableView、它的单元格、iPhone 时间、电池电量图标等保持显示。
navigationController?.setNavigationBarHidden(true, animated: true)
这是我使用 Swift 5, XCode 12.
对我有用的方法
第 1 步(可选)- 创建自定义 UINavigationController class
class CustomNavigationController: UINavigationController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBar.isTranslucent = true
}
用这个 UINavigationController subclass 替换你的 UINavigationController。我将其标记为可选,因为这是基于偏好的,如果你不设置它,你的导航栏将是不透明的,你看不到它下面的内容。
设置navigationBar.isTranslucent = true
可以让你看到它下面的背景,这是我喜欢的。 subclass 也是可选的,但您可能需要对导航栏进行其他更新,所以我总是喜欢将其设为 subclass.
第 2 步 - 设置背景视图约束
class CustomViewController: UIViewController {
// your background view
let bgImageView: UIImageView = {
let bgImageView = UIImageView()
bgImageView.image = UIImage(named: "gradient_background")
bgImageView.contentMode = .scaleAspectFill
return bgImageView
}()
// Get the height of the nav bar and the status bar so you
// know how far up your background needs to go
var topBarHeight: CGFloat {
var top = self.navigationController?.navigationBar.frame.height ?? 0.0
if #available(iOS 13.0, *) {
top += UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
top += UIApplication.shared.statusBarFrame.height
}
return top
}
var isLayoutConfigured = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
title = "Site Visit"
// you only want to do this once
if !isLayoutConfigured() {
isLayoutConfigured = true
configBackground()
}
}
private func configBackground() {
view.addSubview(bgImageView)
configureBackgroundConstraints()
}
// Set up your constraints, main one here is the top constraint
private func configureBackgroundConstraints() {
bgImageView.translatesAutoresizingMaskIntoConstraints = false
bgImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,
constant: -topBarHeight).isActive = true
bgImageView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,
constant: 0).isActive = true
bgImageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: 0).isActive = true
bgImageView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,
constant: 0).isActive = true
view.layoutIfNeeded()
}
设置约束前:
设置以上约束后:
我设法在嵌入导航控制器的 UITableViewController 中创建半透明和圆形的 UITableViewCells,并在 viewDidLoad():
中使用这行代码 tableView.backgroundView = UIImageView(image: UIImage(named: "nightTokyo"))
但我希望背景图片填满整个 phone 屏幕。我将代码(和仅这行代码)更改为:
navigationController?.view = UIImageView(image: UIImage(named: "nightTokyo"))
现在背景图像填满了整个 phone 屏幕,但是我的 table 甚至 iPhone 的时间和电池指示器图标都不见了。
我想要的是背景图片填满整个屏幕,但 tableView、它的单元格、iPhone 时间、电池电量图标等保持显示。
navigationController?.setNavigationBarHidden(true, animated: true)
这是我使用 Swift 5, XCode 12.
对我有用的方法第 1 步(可选)- 创建自定义 UINavigationController class
class CustomNavigationController: UINavigationController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBar.isTranslucent = true
}
用这个 UINavigationController subclass 替换你的 UINavigationController。我将其标记为可选,因为这是基于偏好的,如果你不设置它,你的导航栏将是不透明的,你看不到它下面的内容。
设置navigationBar.isTranslucent = true
可以让你看到它下面的背景,这是我喜欢的。 subclass 也是可选的,但您可能需要对导航栏进行其他更新,所以我总是喜欢将其设为 subclass.
第 2 步 - 设置背景视图约束
class CustomViewController: UIViewController {
// your background view
let bgImageView: UIImageView = {
let bgImageView = UIImageView()
bgImageView.image = UIImage(named: "gradient_background")
bgImageView.contentMode = .scaleAspectFill
return bgImageView
}()
// Get the height of the nav bar and the status bar so you
// know how far up your background needs to go
var topBarHeight: CGFloat {
var top = self.navigationController?.navigationBar.frame.height ?? 0.0
if #available(iOS 13.0, *) {
top += UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
top += UIApplication.shared.statusBarFrame.height
}
return top
}
var isLayoutConfigured = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
title = "Site Visit"
// you only want to do this once
if !isLayoutConfigured() {
isLayoutConfigured = true
configBackground()
}
}
private func configBackground() {
view.addSubview(bgImageView)
configureBackgroundConstraints()
}
// Set up your constraints, main one here is the top constraint
private func configureBackgroundConstraints() {
bgImageView.translatesAutoresizingMaskIntoConstraints = false
bgImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,
constant: -topBarHeight).isActive = true
bgImageView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,
constant: 0).isActive = true
bgImageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: 0).isActive = true
bgImageView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,
constant: 0).isActive = true
view.layoutIfNeeded()
}
设置约束前:
设置以上约束后: