Swift - UITableViewCell class contentView programmatic constraints have no width

class Cell:UITableViewCell {
   var myImage:UIImageView = UIImageView()
   override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        myImage.setTranslatesAutoresizingMaskIntoConstraints(false)
        time.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(userImage)
        contentView.addSubview(time)
        let viewsDict = ["contentView":contentView,"myImage":myImage,"time":time]
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage(50)]-[time]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[myImage(50)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
   }
}

      

I want the time to be attached on the right side of the subview, which is the contentView, but next to myImage: portrait mode even in landscape mode, the ContentView does not reach the whole screen: landscape mode

I tried with contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

but got the error:

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )>
        When added to a view, the constraint items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
    2015-01-13 20:05:11.546 myApp[6117:775684] View hierarchy unprepared for constraint.
        Constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )>
        Container hierarchy: 
    <UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>
       | <UILabel: 0x79e59520; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x79e595e0>>
       | <UIImageView: 0x79e59750; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x79e597d0>>
        View not found in container hierarchy: <myApp.Cell: 0x79e58f00; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x79e59b50>>
        That view superview: NO SUPERVIEW
    2015-01-13 20:05:11.857 myApp[6117:775684] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That illegal. constraint:<NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )> view:<UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>'

      

UPDATE: I am fixing the row width when adding constraints to the tableView, but the label time

is still not on the right side of the contentView. Any suggestions? It looks like the ContentView is no longer than the width of the table. I dont know...

+3


source to share


3 answers


Try adding contentView constraint to supervisor:

contentView.superview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

      



You may also need to call this to render the cell correctly:

self.setTranslatesAutoresizingMaskIntoConstraints(false)

      

+1


source


You can set the text alignment of the labels to the right, then the label will be aligned to the right of the content. Something like that.



time.textAlignment = NSTextAlignment.Right

      

+1


source


I would suggest using the following setting for your constraints (replacing your code starting at the line viewDict

):

let viewsDict = ["contentView":contentView,"myImage":myImage,"time":time]

self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[-10-contentView-10-]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage(50)]-(>=10)-[time]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[myImage(50)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

self.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.setTranslatesAutoresizingMaskIntoConstraints(false)

      

0


source







All Articles