Confused in trying to understand .layoutMarginsGuide inside custom section Header for a tableView
I am trying to create a custom section header for a table view containing a textView. I have created a simple app adapting from a tutorial. This creates a working example. However, when I move the code to my production app, it does not layout correctly. The simple tutorial app creates the layout from nib (no Main.storyboard), the production app does not. I am assuming that is part of the problem. In both I am using .layoutMarginsGuide.
Code follows from the design of the custom header.
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
Notice the layout is driven by:
let margins = contentView.layoutMarginsGuide
And for ease of following the sizing and placement of the TextView I have set the background to yellow.
sectionTextView.backgroundColor = UIColor.yellow
Additional programming is required to register the header in viewDidLoad, which I have done:
override func viewDidLoad()
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.tableView.frame = self.view.bounds
self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(TextViewHeader.self, forHeaderFooterViewReuseIdentifier: TextViewHeader.reuseIdentifer)
self.view.addSubview(self.tableView)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300
And to replace:
tableView(_ tableView:, titleForHeaderInSection section:).
With the following:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TextViewHeader.reuseIdentifer) as? TextViewHeader else
return nil
header.customTextView.text = "Section (section + 1)"
return header
This works and inserts the textView with the layout as expected.
However, the same does not seem to occur when I move the programming to my production app. In this instance I am merging this programatic layout with the layout on the xcode interface builder. Something is interfering with the layout.
In the production app I am using similar code in the custom header design. (included below for completeness)
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
This gives the following layout.
Reviewing the tableView section of my storyboard. I see a Sections Header Height setting that is set to automatic and an Estimate for row height set to zero. Changing that Estimate to automate changes the results:
Settings:
And the resulting header is as shown:
Better but the space above the textView is too large.
Trying to investigate I can see the following after a break.
The margin origin of -8, -8 is confusing to me. Furthermore, that is exactly what I get from my example that works perfectly.
However, when investigating at this break I receive the following warnings:
2018-11-14 15:21:54.086637-0500 appName[31946:1780119] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one
you don't want. Try this: (1) look at each constraint and try to
figure out which you don't expect; (2) find the code that added the
unwanted constraint or constraints and fix it.
This is followed by listing several constraints concerning leading and trailing but not top and bottom. Therefore, no clear indication of what to fix.
Here are the listed constraints
"<NSLayoutConstraint:0x600002364280 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.leading == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x6000023642d0 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.trailing == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000231b840 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x7fe43a815410.width == 0 (active)>",
"<NSLayoutConstraint:0x60000231b4d0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>",
"<NSLayoutConstraint:0x60000231b570 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>"
My only thought is the mixing of "leftMargin" and "rightMargin" and the ".leading" and ".trailing". Is it possible that is the problem? But as discussed the issue is the top and bottom alignment.
My review of the settings on the storyboard for the tableView and the tableView Headers reveal no further information on possible conflicts.
What am I possibly missing?
swift uitableview nslayoutconstraint
add a comment |
I am trying to create a custom section header for a table view containing a textView. I have created a simple app adapting from a tutorial. This creates a working example. However, when I move the code to my production app, it does not layout correctly. The simple tutorial app creates the layout from nib (no Main.storyboard), the production app does not. I am assuming that is part of the problem. In both I am using .layoutMarginsGuide.
Code follows from the design of the custom header.
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
Notice the layout is driven by:
let margins = contentView.layoutMarginsGuide
And for ease of following the sizing and placement of the TextView I have set the background to yellow.
sectionTextView.backgroundColor = UIColor.yellow
Additional programming is required to register the header in viewDidLoad, which I have done:
override func viewDidLoad()
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.tableView.frame = self.view.bounds
self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(TextViewHeader.self, forHeaderFooterViewReuseIdentifier: TextViewHeader.reuseIdentifer)
self.view.addSubview(self.tableView)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300
And to replace:
tableView(_ tableView:, titleForHeaderInSection section:).
With the following:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TextViewHeader.reuseIdentifer) as? TextViewHeader else
return nil
header.customTextView.text = "Section (section + 1)"
return header
This works and inserts the textView with the layout as expected.
However, the same does not seem to occur when I move the programming to my production app. In this instance I am merging this programatic layout with the layout on the xcode interface builder. Something is interfering with the layout.
In the production app I am using similar code in the custom header design. (included below for completeness)
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
This gives the following layout.
Reviewing the tableView section of my storyboard. I see a Sections Header Height setting that is set to automatic and an Estimate for row height set to zero. Changing that Estimate to automate changes the results:
Settings:
And the resulting header is as shown:
Better but the space above the textView is too large.
Trying to investigate I can see the following after a break.
The margin origin of -8, -8 is confusing to me. Furthermore, that is exactly what I get from my example that works perfectly.
However, when investigating at this break I receive the following warnings:
2018-11-14 15:21:54.086637-0500 appName[31946:1780119] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one
you don't want. Try this: (1) look at each constraint and try to
figure out which you don't expect; (2) find the code that added the
unwanted constraint or constraints and fix it.
This is followed by listing several constraints concerning leading and trailing but not top and bottom. Therefore, no clear indication of what to fix.
Here are the listed constraints
"<NSLayoutConstraint:0x600002364280 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.leading == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x6000023642d0 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.trailing == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000231b840 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x7fe43a815410.width == 0 (active)>",
"<NSLayoutConstraint:0x60000231b4d0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>",
"<NSLayoutConstraint:0x60000231b570 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>"
My only thought is the mixing of "leftMargin" and "rightMargin" and the ".leading" and ".trailing". Is it possible that is the problem? But as discussed the issue is the top and bottom alignment.
My review of the settings on the storyboard for the tableView and the tableView Headers reveal no further information on possible conflicts.
What am I possibly missing?
swift uitableview nslayoutconstraint
add a comment |
I am trying to create a custom section header for a table view containing a textView. I have created a simple app adapting from a tutorial. This creates a working example. However, when I move the code to my production app, it does not layout correctly. The simple tutorial app creates the layout from nib (no Main.storyboard), the production app does not. I am assuming that is part of the problem. In both I am using .layoutMarginsGuide.
Code follows from the design of the custom header.
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
Notice the layout is driven by:
let margins = contentView.layoutMarginsGuide
And for ease of following the sizing and placement of the TextView I have set the background to yellow.
sectionTextView.backgroundColor = UIColor.yellow
Additional programming is required to register the header in viewDidLoad, which I have done:
override func viewDidLoad()
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.tableView.frame = self.view.bounds
self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(TextViewHeader.self, forHeaderFooterViewReuseIdentifier: TextViewHeader.reuseIdentifer)
self.view.addSubview(self.tableView)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300
And to replace:
tableView(_ tableView:, titleForHeaderInSection section:).
With the following:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TextViewHeader.reuseIdentifer) as? TextViewHeader else
return nil
header.customTextView.text = "Section (section + 1)"
return header
This works and inserts the textView with the layout as expected.
However, the same does not seem to occur when I move the programming to my production app. In this instance I am merging this programatic layout with the layout on the xcode interface builder. Something is interfering with the layout.
In the production app I am using similar code in the custom header design. (included below for completeness)
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
This gives the following layout.
Reviewing the tableView section of my storyboard. I see a Sections Header Height setting that is set to automatic and an Estimate for row height set to zero. Changing that Estimate to automate changes the results:
Settings:
And the resulting header is as shown:
Better but the space above the textView is too large.
Trying to investigate I can see the following after a break.
The margin origin of -8, -8 is confusing to me. Furthermore, that is exactly what I get from my example that works perfectly.
However, when investigating at this break I receive the following warnings:
2018-11-14 15:21:54.086637-0500 appName[31946:1780119] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one
you don't want. Try this: (1) look at each constraint and try to
figure out which you don't expect; (2) find the code that added the
unwanted constraint or constraints and fix it.
This is followed by listing several constraints concerning leading and trailing but not top and bottom. Therefore, no clear indication of what to fix.
Here are the listed constraints
"<NSLayoutConstraint:0x600002364280 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.leading == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x6000023642d0 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.trailing == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000231b840 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x7fe43a815410.width == 0 (active)>",
"<NSLayoutConstraint:0x60000231b4d0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>",
"<NSLayoutConstraint:0x60000231b570 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>"
My only thought is the mixing of "leftMargin" and "rightMargin" and the ".leading" and ".trailing". Is it possible that is the problem? But as discussed the issue is the top and bottom alignment.
My review of the settings on the storyboard for the tableView and the tableView Headers reveal no further information on possible conflicts.
What am I possibly missing?
swift uitableview nslayoutconstraint
I am trying to create a custom section header for a table view containing a textView. I have created a simple app adapting from a tutorial. This creates a working example. However, when I move the code to my production app, it does not layout correctly. The simple tutorial app creates the layout from nib (no Main.storyboard), the production app does not. I am assuming that is part of the problem. In both I am using .layoutMarginsGuide.
Code follows from the design of the custom header.
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
Notice the layout is driven by:
let margins = contentView.layoutMarginsGuide
And for ease of following the sizing and placement of the TextView I have set the background to yellow.
sectionTextView.backgroundColor = UIColor.yellow
Additional programming is required to register the header in viewDidLoad, which I have done:
override func viewDidLoad()
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.tableView.frame = self.view.bounds
self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(TextViewHeader.self, forHeaderFooterViewReuseIdentifier: TextViewHeader.reuseIdentifer)
self.view.addSubview(self.tableView)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300
And to replace:
tableView(_ tableView:, titleForHeaderInSection section:).
With the following:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TextViewHeader.reuseIdentifer) as? TextViewHeader else
return nil
header.customTextView.text = "Section (section + 1)"
return header
This works and inserts the textView with the layout as expected.
However, the same does not seem to occur when I move the programming to my production app. In this instance I am merging this programatic layout with the layout on the xcode interface builder. Something is interfering with the layout.
In the production app I am using similar code in the custom header design. (included below for completeness)
class TextViewHeader: UITableViewHeaderFooterView
static let reuseIdentifer = "TextViewHeaderReuseIdentifier"
let sectionTextView = UITextView.init()
override public init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
sectionTextView.font = UIFont.preferredFont(forTextStyle: .title2)
sectionTextView.textColor = UIColor.blue
sectionTextView.backgroundColor = UIColor.yellow
sectionTextView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(sectionTextView)
let margins = contentView.layoutMarginsGuide
sectionTextView.isScrollEnabled = false
//sectionTextView.text = "Something"
sectionTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
sectionTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
sectionTextView.frameLayoutGuide.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
sectionTextView.frameLayoutGuide.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
This gives the following layout.
Reviewing the tableView section of my storyboard. I see a Sections Header Height setting that is set to automatic and an Estimate for row height set to zero. Changing that Estimate to automate changes the results:
Settings:
And the resulting header is as shown:
Better but the space above the textView is too large.
Trying to investigate I can see the following after a break.
The margin origin of -8, -8 is confusing to me. Furthermore, that is exactly what I get from my example that works perfectly.
However, when investigating at this break I receive the following warnings:
2018-11-14 15:21:54.086637-0500 appName[31946:1780119] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one
you don't want. Try this: (1) look at each constraint and try to
figure out which you don't expect; (2) find the code that added the
unwanted constraint or constraints and fix it.
This is followed by listing several constraints concerning leading and trailing but not top and bottom. Therefore, no clear indication of what to fix.
Here are the listed constraints
"<NSLayoutConstraint:0x600002364280 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.leading == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x6000023642d0 UILayoutGuide:0x6000039257a0'UIScrollView-frameLayoutGuide'.trailing == UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000231b840 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x7fe43a815410.width == 0 (active)>",
"<NSLayoutConstraint:0x60000231b4d0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>",
"<NSLayoutConstraint:0x60000231b570 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60000397a060'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UITableViewHeaderFooterContentView:0x7fe43a815410 )>"
My only thought is the mixing of "leftMargin" and "rightMargin" and the ".leading" and ".trailing". Is it possible that is the problem? But as discussed the issue is the top and bottom alignment.
My review of the settings on the storyboard for the tableView and the tableView Headers reveal no further information on possible conflicts.
What am I possibly missing?
swift uitableview nslayoutconstraint
swift uitableview nslayoutconstraint
asked Nov 14 '18 at 20:59
jz_jz_
288
288
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I have found this to be a problematic approach. The NSLayoutConstraint issues are beyond my Swift knowledge at this time. It seems to be driven by the time events occur. Furthermore, there is some discussion that it is somewhat buggy. See the following: What is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly?
I decided to forgo the use of tableView.dequeueReusableHeaderFooterView. The number of sections will be limited (2 or 3, in the vast amount to uses) so the costs will be minimal.
Therefore, I implemented a straightforward method.
//define size of font to use
let sectionFont = UIFont.preferredFont(forTextStyle: .title3)
let fontsize = UIFont.preferredFont(forTextStyle: .title3).pointSize
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: fontsize+10))
let textView = UITextView()
textView.frame = CGRect.init(x: 0, y: -10, width: headerView.frame.width-10, height: headerView.frame.height-0)
textView.text = "Section (section)"
textView.font = sectionFont
headerView.addSubview(textView)
return headerView
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return fontsize+10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f53308621%2fconfused-in-trying-to-understand-layoutmarginsguide-inside-custom-section-heade%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have found this to be a problematic approach. The NSLayoutConstraint issues are beyond my Swift knowledge at this time. It seems to be driven by the time events occur. Furthermore, there is some discussion that it is somewhat buggy. See the following: What is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly?
I decided to forgo the use of tableView.dequeueReusableHeaderFooterView. The number of sections will be limited (2 or 3, in the vast amount to uses) so the costs will be minimal.
Therefore, I implemented a straightforward method.
//define size of font to use
let sectionFont = UIFont.preferredFont(forTextStyle: .title3)
let fontsize = UIFont.preferredFont(forTextStyle: .title3).pointSize
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: fontsize+10))
let textView = UITextView()
textView.frame = CGRect.init(x: 0, y: -10, width: headerView.frame.width-10, height: headerView.frame.height-0)
textView.text = "Section (section)"
textView.font = sectionFont
headerView.addSubview(textView)
return headerView
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return fontsize+10
add a comment |
I have found this to be a problematic approach. The NSLayoutConstraint issues are beyond my Swift knowledge at this time. It seems to be driven by the time events occur. Furthermore, there is some discussion that it is somewhat buggy. See the following: What is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly?
I decided to forgo the use of tableView.dequeueReusableHeaderFooterView. The number of sections will be limited (2 or 3, in the vast amount to uses) so the costs will be minimal.
Therefore, I implemented a straightforward method.
//define size of font to use
let sectionFont = UIFont.preferredFont(forTextStyle: .title3)
let fontsize = UIFont.preferredFont(forTextStyle: .title3).pointSize
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: fontsize+10))
let textView = UITextView()
textView.frame = CGRect.init(x: 0, y: -10, width: headerView.frame.width-10, height: headerView.frame.height-0)
textView.text = "Section (section)"
textView.font = sectionFont
headerView.addSubview(textView)
return headerView
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return fontsize+10
add a comment |
I have found this to be a problematic approach. The NSLayoutConstraint issues are beyond my Swift knowledge at this time. It seems to be driven by the time events occur. Furthermore, there is some discussion that it is somewhat buggy. See the following: What is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly?
I decided to forgo the use of tableView.dequeueReusableHeaderFooterView. The number of sections will be limited (2 or 3, in the vast amount to uses) so the costs will be minimal.
Therefore, I implemented a straightforward method.
//define size of font to use
let sectionFont = UIFont.preferredFont(forTextStyle: .title3)
let fontsize = UIFont.preferredFont(forTextStyle: .title3).pointSize
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: fontsize+10))
let textView = UITextView()
textView.frame = CGRect.init(x: 0, y: -10, width: headerView.frame.width-10, height: headerView.frame.height-0)
textView.text = "Section (section)"
textView.font = sectionFont
headerView.addSubview(textView)
return headerView
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return fontsize+10
I have found this to be a problematic approach. The NSLayoutConstraint issues are beyond my Swift knowledge at this time. It seems to be driven by the time events occur. Furthermore, there is some discussion that it is somewhat buggy. See the following: What is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly?
I decided to forgo the use of tableView.dequeueReusableHeaderFooterView. The number of sections will be limited (2 or 3, in the vast amount to uses) so the costs will be minimal.
Therefore, I implemented a straightforward method.
//define size of font to use
let sectionFont = UIFont.preferredFont(forTextStyle: .title3)
let fontsize = UIFont.preferredFont(forTextStyle: .title3).pointSize
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: fontsize+10))
let textView = UITextView()
textView.frame = CGRect.init(x: 0, y: -10, width: headerView.frame.width-10, height: headerView.frame.height-0)
textView.text = "Section (section)"
textView.font = sectionFont
headerView.addSubview(textView)
return headerView
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return fontsize+10
answered Nov 18 '18 at 14:30
jz_jz_
288
288
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.
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%2f53308621%2fconfused-in-trying-to-understand-layoutmarginsguide-inside-custom-section-heade%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