iOS - subivew can't be centered in UITableViewCell - ios

It's really weird.
No matter how I set the constraints, it just ignors all of them after layouts.
I've tried to use cell.indicator.center = cell.center and cell.indicator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin] in the func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
but still not worked.
Is there any special way to center a subview inside a UITableViewCell?
LoadingCell.xib
Screenshot(Simulator)
updated:
After trying #Vibha Singh's advice, I put these codes in my LoadingCell.
override func layoutSubviews() {
print("layoutSubviews")
print(center)
print(contentView.bounds)
indicator.center = contentView.center
}
And it printed these lines:
layoutSubviews
(207.0, 1055.16668891907)
(0.0, 0.0, 414.0, 44.6666666666667)
But the indicator is still not centered.
updated:
I fixed it by creating a new cell with an indicator.
Both of them have exactly the same constraints. The new one is centered as expected, but the old one is still positioned at left top.
updated:
I did use centerX and centerY as constraints at the first time. But it's not worked. So I have to try another way. That's why I use so many constraints in the first screenshot.
Here are the screenshots with two exactly the same xibs.
Both of them use the same codes to dequeue.
let dequeue = tableView.dequeueReusableCell(withIdentifier: SharedCell.loading.rawValue, for: indexPath)
if let cell = dequeue as? CenterLoadingCell {
cell.indicator.startAnimating()
}
else if let cell = dequeue as? LoadingCell {
cell.indicator.startAnimating()
}
return dequeue
The first one is named LoadingCell, which is not centered on the simulator.
The second one is named CenterLoadingCell, which I created after I asked this question. And this one is centered on the simulator.

You are confusing your layout setting big time by adding unnecessary constraints. Check this implementation.
With Custom Cell:

You can add indicator like
let activityView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityView.center = CGPoint(x: CGFloat(cell.bounds.midX), y: CGFloat(cell.bounds.midY))
cell.contentView.addSubview(activityView)
activityView.startAnimating()
Or you can set frame in below method of cell
override func layoutSubviews()
{
}

You can make it easily with constraints
func setConstraints(item:UIView , relatedTo:UIView , attr:NSLayoutAttribute , relatedBy:NSLayoutRelation , multiplier: CGFloat , constant : CGFloat)
{
relatedTo.addSubview(item)
item.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: item, attribute: attr, relatedBy: relatedBy, toItem: relatedTo, attribute: attr, multiplier: multiplier, constant: constant).isActive = true
}
and usage :
setConstraints(item: indicator, relatedTo:cell.contentView, attr:centerX, relatedBy:.equal, multiplier: 0 , constant : 0)

Related

How to create "real" UITableView(Cell) margins where cells cannot be selected outside their content frame?

I would like to create a UITableView layout where the table view itself fills the complete screen (-> scrollbars are shown at screen endges) but the cells are horizontally centered with a fixed width. It should only be possible to select a cell / tap on it within this fixed width but not within the margins:
Simply giving the UITableView and fixed with + center alignment does work, but in this case the scrollbars are not at the screen edges and it is not possible to scroll using the complete screen but only within the tableView frame.
I tried different other solutions:
let cellWidth = 200
let widthDiff = (tableView.frame.width - cellWidth) / 2
// Solution 1: Change horizontal content inset
tableView.contentInset.left = widthDiff
tableView.contentInset.rigth = widthDiff
// => Cells still use the complete width but can be scrolled horizontally by widthDiff
// => Does NOT work
// Solution 2: Setting layout margins on tableView
tableView.layoutMargins.left = widthDiff
tableView.layoutMargins.rigth = widthDiff
// Solution 3: Setting layout margins on cells...
...
cell.layoutMargins.left = widthDiff
cell.layoutMargins.rigth = widthDiff
// Solution 4: Manually center the cell content using constraints within the cell layout.
// => Layout looks correct in all three cases, but while the cell content
// has the correct margins, the cells itselfs still use the complete
// screen width and is still possible to tap/select sells outside
// their frame.
// => Does NOT work
So, I was not able to find a solution which fulfills all three requirements:
Fixed width, centered cells with left and right margins
Scrollbars at screen edges / table view can be scrolled using the complete screen
Cells can only be selected / tapped on within their content frame
Is there a solution using UITableView properties and methods?
EDIT: As requested this image shows what it should look like:
Cells are centered in the middle with some margins on both sides
Scrollbars are at the screen endge
But: The cells still occupy the complete width. When tapping in the area of the margins cell is still selected and its selected-background uses the complete width. This should be avoided.
From touches point of view you seem to want that cells are selectable only at specific position but table view can be selected everywhere where table view is (for scrolling).
From views point of view you wish to limit cells to specific location but want to draw table view everywhere (scrollbars at the edge).
Then I would say that table view needs to stretch through whole screen and the cell content should be limited. This would best be done with simply constraining a custom view within your cell. The selection of cells would then need to be custom. Consider something like the following:
(I intentionally did some parts programmatically to show what is being done. But I would put most of this in storyboard).
class ViewController: UIViewController {
#IBOutlet private var tableView: UITableView?
var dataSource: [Bool] = []
var cellWidth: CGFloat = 200.0
override func viewDidLoad() {
super.viewDidLoad()
tableView?.allowsSelection = false
dataSource = .init(repeating: false, count: 100)
tableView?.reloadData()
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyCell()
cell.setupIfNeeded(width: cellWidth)
cell.isCellSelected = dataSource[indexPath.row]
cell.onMiddleViewPressed = { [weak self, weak cell] in
self?.dataSource[indexPath.row] = true
cell?.isCellSelected = true
}
return cell
}
}
class MyCell: UITableViewCell {
private var isSetup: Bool = false
private var middleView: UIView?
var onMiddleViewPressed: (() -> Void)?
var isCellSelected: Bool = false {
didSet {
middleView?.backgroundColor = isCellSelected ? .blue : .gray
}
}
func setupIfNeeded(width: CGFloat) {
guard isSetup == false else { return }
isSetup = true
let middleView = UIView(frame: .zero)
middleView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(middleView)
contentView.addConstraint(.init(item: middleView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1.0, constant: 0.0))
middleView.addConstraint(.init(item: middleView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width))
contentView.addConstraint(.init(item: middleView, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(.init(item: middleView, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
middleView.backgroundColor = isCellSelected ? .blue : .gray
middleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onMiddleViewSelected)))
self.middleView = middleView
}
#objc private func onMiddleViewSelected() {
onMiddleViewPressed?()
}
}
To create a toggle in selection you would then simply do:
cell.onMiddleViewPressed = { [weak self, weak cell] in
guard let self = self else { return }
self.dataSource[indexPath.row].toggle()
cell?.isCellSelected = self.dataSource[indexPath.row]
}
To use a single selection you would do
cell.onMiddleViewPressed = { [weak self, weak cell, weak tableView] in
guard let self = self else { return }
if let currentSelectionIndex = self.dataSource.firstIndex(of: true), currentSelectionIndex != indexPath.row {
self.dataSource[currentSelectionIndex] = false
let targetIndexPath = IndexPath(row: currentSelectionIndex, section: 0)
if tableView?.indexPathsForVisibleRows?.contains(targetIndexPath) == true {
tableView?.reloadRows(at: [targetIndexPath], with: .none)
}
}
self.dataSource[indexPath.row].toggle()
cell?.isCellSelected = self.dataSource[indexPath.row]
}
so this is nothing too heavy. And a pretty standard procedures in cases like having UISwitch (or similar components) for selection where selecting the cell does something completely different (navigate to details for instance).
Perhaps you also need transparent cells and table view. This is just:
tableView?.backgroundColor = .clear
cell.backgroundColor = .clear
cell.contentView.backgroundColor = .clear

UICollectionView .reloadData() only shows sections

I have a collection view which has a section with a title and every section has some words. Words differ in size.
Because the words differ in size i've added the following, to prevent long words from being cut off: layout.estimatedItemSize = CGSize(width: 1.0, height: 1.0)
However after setting that and invoking reloadData(), the cells (words) do not get loaded only the sections (title).
But after scrolling all the sections that went out of screen will load their words. However when I don't use layout.estimatedItemSize it works correctly, but the words are cut off.
So my question is if there is another way to display those words (which are basically a small view with a label) without them being cut off. Or am I using estimatedSize wrongly?
As I read from the docs from apple itself: docs
The default value of this property is CGSizeZero. Setting it to any other value causes the collection view to query each cell for its actual size using the cell’s preferredLayoutAttributesFitting(_:) method.
I do set constraints dynamically and statically (StoryBoard),
my dynamic constraint is as following:
if prevCell != nil {
let constraint = NSLayoutConstraint(item: cell, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: prevCell, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 5.0)
cell.addConstraint(constraint)
self.prevCell = cell
}
Things I have tried myself so far:
//before reload data invalidate the layout
self.collectionView!.collectionViewLayout.invalidateLayout()
self.collectionView!.reloadData()
After some more research found the correct way to handle this.
I forgot to override the preferredLayoutAttributesFitting in the cell's class. To make it work I just left the layout.estimatedItemSize = CGSize(width: 1.0, height: 1.0) in viewDidLoad of the collection view.
However somehow this is not enough because it wont be able to calculate the right size not until you scroll it out of screen. It probably has some logic behind it that I do not know of.
But when overriding the the preferredLayoutAttributesFitting as follow:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let updatedSize = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var updatedFrame = layoutAttributes.frame
updatedFrame.size.width = CGFloat(ceilf(Float(updatedSize.width)))
updatedFrame.size.height = CGFloat(ceilf(Float(updatedSize.height)))
layoutAttributes.frame = updatedFrame
return layoutAttributes
}

Adding Right Aligning Constraints to the Programmatically Added Subview

I am trying to add a right aligned Segmented Control to the `UITableViewCell like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "mycell")
// ...
addSegmentedControlToCell(cell)
// ...
and then
func addSegmentedControlToCell(_ cell:UITableViewCell){
let items = ["One","Two"]
let unitControl = UISegmentedControl(items: items)
unitControl.selectedSegmentIndex = 0
let maxWd = cell.frame.size.width
let maxHt = cell.frame.size.height
let padding:CGFloat = 5
unitControl.frame = CGRect(x:maxWd/2, y:padding, width:maxWd/2, height: maxHt-padding*2)
unitControl.addTarget(self, action: #selector(SettingsTableViewController.unitControlValueDidChange(_:)), for: .valueChanged)
cell.addSubview(unitControl)
}
This works well on my default device iPhone 6. But when I run this app on a smaller width device, like iPhone 5, the programmatically added Segment Control, which receives 50% of cell width (cell.frame.size.width/2) seems much bigger that 50% of width and stretches to the right under the cell view port.
This is because of auto-layout and constraints as I see because iPhone 5 cell view gets resized. So I am trying to add a constraint to my new Segment Control, which fails with app crush. Note, I am not very good with programmatically adding constraints yet.
let widthContr = NSLayoutConstraint(item: unitControl,
attribute: NSLayoutAttribute.width,
relatedBy: NSLayoutRelation.equal,
toItem: cell,
attribute: NSLayoutAttribute.notAnAttribute,
multiplier: 1,
constant: 0.5)
cell.addConstraints([widthContr])
How to right align subview correctly (Segment Control) for correct cell size?
You can set constraints like this:
yourView.rightAnchor.constraint(equalTo: yourCell.rightAnchor, constant: -10).isActive = true
Or:
yourView.heightAnchor.constraint(equalToConstant: 50).isActive = true
But make sure that you include this code:
yourSegmentedControl.translatesAutoresizingMaskIntoConstraints = false
You can do this in 3 ways.
1) You have written 0.5 constant in NSLayoutConstraint(this is a mistake).
You need to write constant is 1 and multiplier should be 0.5.
2) Or you should update frame of UISegmentControl in layoutSubviews() method of Custom Cell.
3) Or you should write cell.layoutSubviews() after UISegmentControl adding to Cell.

Two vertically aligned views start differently when their shared parent view embedded in Navigation Controller

I have two table views within a view controller, and the view controller is embedded in a navigation controller.
I have added the following constraints vertically, and I expect the two table views start at the same point, near the bottom of the navigation bar.
constraints.append(t1.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor, constant: 8.0))
constraints.append(t1.bottomAnchor.constraintEqualToAnchor(self.bottomLayoutGuide.topAnchor, constant: -8.0))
constraints.append(t2.topAnchor.constraintEqualToAnchor(t1.topAnchor))
constraints.append(t2.bottomAnchor.constraintEqualToAnchor(t1.bottomAnchor))
However, it turns out the table view t1 starts much lower than the table view t2, the latter of which starts near the bottom of the navigation bar as expected.
Why does this happen? How to fix this?
UPDATE
Constraints:
t1.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
t2.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
t1.dataSource = self
t2.dataSource = self
self.view.addSubview(t1)
self.view.addSubview(t2)
t1.translatesAutoresizingMaskIntoConstraints = false
t2.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
constraints.append(t1.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor, constant: 8.0))
constraints.append(t1.bottomAnchor.constraintEqualToAnchor(self.bottomLayoutGuide.topAnchor, constant: -8.0))
constraints.append(t2.topAnchor.constraintEqualToAnchor(t1.topAnchor))
constraints.append(t2.bottomAnchor.constraintEqualToAnchor(t1.bottomAnchor))
constraints.append(t1.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor, constant: 8.0))
constraints.append(t2.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor, constant: -8.0))
constraints.append(t2.leadingAnchor.constraintEqualToAnchor(t1.trailingAnchor, constant: 8.0))
constraints.append(t1.widthAnchor.constraintEqualToAnchor(t2.widthAnchor))
NSLayoutConstraint.activateConstraints(constraints)
Data Sources:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "Start"
return cell
}
Unexpected Alignments:
just tried to replicate the issue.
The table views get perfectly aligned if I use the code below. Just added the constraints in the view controller.
t1.backgroundColor = UIColor.redColor()
t2.backgroundColor = UIColor.blueColor()
view.addSubview(t1)
view.addSubview(t2)
t1.translatesAutoresizingMaskIntoConstraints = false
t2.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(t1.leftAnchor.constraintEqualToAnchor(view.leftAnchor, constant: 0.0))
view.addConstraint(t2.rightAnchor.constraintEqualToAnchor(view.rightAnchor, constant: 0.0))
t1.addConstraint(NSLayoutConstraint(item: t1, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0))
t2.addConstraint(NSLayoutConstraint(item: t2, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0))
view.addConstraint(t1.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8.0))
view.addConstraint(t1.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -8.0))
view.addConstraint(t2.topAnchor.constraintEqualToAnchor(t1.topAnchor))
view.addConstraint(t2.bottomAnchor.constraintEqualToAnchor(t1.bottomAnchor))
Just gives me this layout:
Maybe you can give more insights into your whole layout code?
UPDATE
After investigating the issue with your provided layout code:
The table views are layouted exactly as expected. The problem is with the
contentInset.
Add this for some console logs:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("Insets t1 -> \(t1.contentInset)")
print("Insets t2 -> \(t2.contentInset)")
}
This prints out:
Insets t1 -> UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
Insets t2 -> UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
There is an option in Interface Builder for your view controller named Adjust Scroll View Insets. Disable it (or use automaticallyAdjustsScrollViewInsets = false in code).
In general table views (as can be seen in table view controllers) go under the navigation bar (so you get the blurring scroll effect...) but the content inset is set so that your first cell is below the navigation bar.
To avoid this behavior when referencing the top layout guide you have to disable this option. The other table view is not affected because it only references the layout constraints from the other one.
It is always very helpful to color the views, so you can see if misalignments come from your layout code or any other side effect. (Or use the view debugger from Xcode)
So after setting the flag it gets from this:
to this:
Cheers
Orlando 🍻
Modify one line code would resolve it.
Replace
NSLayoutConstraint.activateConstraints(constraints)
with
self.view.addConstraints(constraints)
I think you don't understand the concept of Auto Layout completely.
UPDATE:
Sorry about that I did't mean to the UINavigationController.
UIViewController defaultly adjust scrollview's inset, you can prevent it via automaticallyAdjustsScrollViewInsets = false.
BTW, you can use Mansory or Purelayout to set constraints easily.
class ViewController: UIViewController, UITableViewDataSource {
var t1: UITableView = UITableView(frame: CGRectZero, style: .Plain)
var t2: UITableView = UITableView(frame: CGRectZero, style: .Plain)
override func viewDidLoad() {
super.viewDidLoad()
// This line is the point.
automaticallyAdjustsScrollViewInsets = false
commonInit()
}
func commonInit(){
view.backgroundColor = UIColor .whiteColor()
t1.backgroundColor = UIColor.blueColor()
t2.backgroundColor = UIColor.greenColor()
view.addSubview(t1)
view.addSubview(t2)
t1.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
t2.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
t1.dataSource = self
t2.dataSource = self
self.view.addSubview(t1)
self.view.addSubview(t2)
t1.translatesAutoresizingMaskIntoConstraints = false
t2.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
constraints.append(t1.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor, constant: 8.0))
constraints.append(t1.bottomAnchor.constraintEqualToAnchor(self.bottomLayoutGuide.topAnchor, constant: -8.0))
constraints.append(t2.topAnchor.constraintEqualToAnchor(t1.topAnchor))
constraints.append(t2.bottomAnchor.constraintEqualToAnchor(t1.bottomAnchor))
constraints.append(t1.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor, constant: 8.0))
constraints.append(t2.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor, constant: -8.0))
constraints.append(t2.leadingAnchor.constraintEqualToAnchor(t1.trailingAnchor, constant: 8.0))
constraints.append(t1.widthAnchor.constraintEqualToAnchor(t2.widthAnchor))
view.addConstraints(constraints)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "Start"
return cell
}
}

Correctly returning UIView in viewForHeaderInSection iOS 8 swift

I am fairly new to swift language and iOS development as a whole so please pardon me for my lack of fundamental knowledge. Previously i tried and successfully Implemented multiple sectioned UITableView custom sections by making a xib file creating TableViewCell and then loading it into my main ViewController and returning it as coded below:
var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView
return customView
But since i started getting "no index path for table cell being reused" i went back to drawing board and tried to do things programmatically creating a UIView and return it, so far i am have been unsuccessful however this is what i coded:
func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!{
if(section == 0) {
var view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))
var label = UILabel(frame: CGRectMake(0,0, tableView.frame.size.width/2, 20))
label.text="My Details"
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(0, 0, tableView.frame.size.width/2, 20)
button.addTarget(self, action: "visibleRow", forControlEvents:.TouchUpInside)
label.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["label": label,"button":button,"view": view]
var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label(20)]-60-[button(20)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
view.addConstraints(horizontallayoutContraints)
return view
}
...
As you can see i am trying to create a layout where i want my label and button horizontally laid out but somehow logic is not working out i tried disabling autoresize constraints on view itself but that too did not worked too. Please Help!
You never added your label or button to "view". Also your sizes don't make sense -- you set the widths of the label and button to be 1/2 the width of the table view, but then in your constraints, you have 80 points worth of spaces, so that can't work. In any case, you should NOT set any frames when you use auto layout. Get rid of those, and add a vertical constraint to either the label or the button, and a layout option to align them vertically. Also, you need to add your label and button to your view (and do it before you add the constraints). Something like this should work,
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if(section == 0) {
var view = UIView() // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath:
var label = UILabel()
label.text="My Details"
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.addTarget(self, action: "visibleRow:", forControlEvents:.TouchUpInside)
label.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTitle("Test Title", forState: .Normal)
let views = ["label": label,"button":button,"view": view]
view.addSubview(label)
view.addSubview(button)
var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", options: .AlignAllCenterY, metrics: nil, views: views)
view.addConstraints(horizontallayoutContraints)
var verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
view.addConstraint(verticalLayoutContraint)
return view
}
return nil
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor.clearColor()
return view
}

Resources