Safe Area Layout Guide Not Working On UITableView's BackgroundView
up vote
0
down vote
favorite
The issue I'm having is that the label is anchored to the bottom edge of the screen when it should be anchored to the safe area layout guide. which will bring the label above the iPhone line.
Here's the code...
class CustomTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = CustomBackgroundView()
}
}
.
class CustomBackgroundView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
let label = UILabel()
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}

swift autolayout safearealayoutguide
add a comment |
up vote
0
down vote
favorite
The issue I'm having is that the label is anchored to the bottom edge of the screen when it should be anchored to the safe area layout guide. which will bring the label above the iPhone line.
Here's the code...
class CustomTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = CustomBackgroundView()
}
}
.
class CustomBackgroundView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
let label = UILabel()
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}

swift autolayout safearealayoutguide
What is your problem?
– Ammar
Nov 21 at 21:44
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The issue I'm having is that the label is anchored to the bottom edge of the screen when it should be anchored to the safe area layout guide. which will bring the label above the iPhone line.
Here's the code...
class CustomTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = CustomBackgroundView()
}
}
.
class CustomBackgroundView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
let label = UILabel()
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}

swift autolayout safearealayoutguide
The issue I'm having is that the label is anchored to the bottom edge of the screen when it should be anchored to the safe area layout guide. which will bring the label above the iPhone line.
Here's the code...
class CustomTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = CustomBackgroundView()
}
}
.
class CustomBackgroundView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
let label = UILabel()
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}

swift autolayout safearealayoutguide
swift autolayout safearealayoutguide
edited Nov 21 at 21:47
asked Nov 21 at 21:33
damiancesar
677517
677517
What is your problem?
– Ammar
Nov 21 at 21:44
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49
add a comment |
What is your problem?
– Ammar
Nov 21 at 21:44
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49
What is your problem?
– Ammar
Nov 21 at 21:44
What is your problem?
– Ammar
Nov 21 at 21:44
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You are seeing this behavior because every UIView has its own SafeAreaLayoutGuide. Per default the SafeAreaLayoutGuide of a generic UIView subclass does not include the Safe Areas that you are looking for. You have to use the SafeAreaLayoutGuide of your table view.
You could do something like this:
class CustomBackgroundView: UIView {
var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
didSet {
guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
}
}
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
}
And then in your UITableViewController do this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let customBackgroundView = CustomBackgroundView()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = customBackgroundView
customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are seeing this behavior because every UIView has its own SafeAreaLayoutGuide. Per default the SafeAreaLayoutGuide of a generic UIView subclass does not include the Safe Areas that you are looking for. You have to use the SafeAreaLayoutGuide of your table view.
You could do something like this:
class CustomBackgroundView: UIView {
var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
didSet {
guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
}
}
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
}
And then in your UITableViewController do this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let customBackgroundView = CustomBackgroundView()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = customBackgroundView
customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}
add a comment |
up vote
0
down vote
You are seeing this behavior because every UIView has its own SafeAreaLayoutGuide. Per default the SafeAreaLayoutGuide of a generic UIView subclass does not include the Safe Areas that you are looking for. You have to use the SafeAreaLayoutGuide of your table view.
You could do something like this:
class CustomBackgroundView: UIView {
var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
didSet {
guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
}
}
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
}
And then in your UITableViewController do this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let customBackgroundView = CustomBackgroundView()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = customBackgroundView
customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You are seeing this behavior because every UIView has its own SafeAreaLayoutGuide. Per default the SafeAreaLayoutGuide of a generic UIView subclass does not include the Safe Areas that you are looking for. You have to use the SafeAreaLayoutGuide of your table view.
You could do something like this:
class CustomBackgroundView: UIView {
var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
didSet {
guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
}
}
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
}
And then in your UITableViewController do this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let customBackgroundView = CustomBackgroundView()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = customBackgroundView
customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}
You are seeing this behavior because every UIView has its own SafeAreaLayoutGuide. Per default the SafeAreaLayoutGuide of a generic UIView subclass does not include the Safe Areas that you are looking for. You have to use the SafeAreaLayoutGuide of your table view.
You could do something like this:
class CustomBackgroundView: UIView {
var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
didSet {
guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
}
}
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupSubviews() {
label.text = "Hello, World!"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
}
And then in your UITableViewController do this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let customBackgroundView = CustomBackgroundView()
tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = customBackgroundView
customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}
answered Nov 22 at 9:15
joern
20.3k66380
20.3k66380
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420760%2fsafe-area-layout-guide-not-working-on-uitableviews-backgroundview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is your problem?
– Ammar
Nov 21 at 21:44
I've updated the post. Check it out.
– damiancesar
Nov 21 at 21:49