1

I'm completely new to UILayoutGuide. I'd like to position a UILabel in the middle of the white space below the imageView, which is the superview of both imageView and UILabel (see view hierarchy below).

enter image description here

Normally, I would add another view and set top constraint to UIImageView and bottom constraint to the bottom edge, then center the label both horizonally and vertically to the view.

But I decided to achieve it using UILayoutGuide. Here is my code:

override func awakeFromNib() {
        super.awakeFromNib()
        let topGuide = UILayoutGuide()
        let bottomGuide = UILayoutGuide()
        self.view.addLayoutGuide(topGuide)
        self.view.addLayoutGuide(bottomGuide)
        topGuide.bottomAnchor.constraint(equalTo: title.topAnchor).isActive = true
        bottomGuide.topAnchor.constraint(equalTo: title.bottomAnchor).isActive = true
        self.view.layoutIfNeeded()
}

The result is that my UILabel does not show at all. What am I doing wrong?

theDC
  • 6,364
  • 10
  • 56
  • 98
  • want using storyboard or programmatically ? – iOS Geek Aug 29 '17 at 04:44
  • There are no layout guides in Interface Builder so what is the screen shot for? Also note that the screen shot tells you your constraints are illegal. – matt Aug 29 '17 at 12:13

1 Answers1

0

enter image description here

Follow Steps -

1) Add a imageView with leading,trailing and top constraint = 0 and give aspect ratio to image so it will automatically get size according to cell

enter image description here

2) now add a uiView just below your imageView and give it 0 constraints as trailing, bottom, leading and 0 contraint to image bottom as shown below and make uiView colour as Clear so it won't show up

enter image description here

3) now drag a label in UiView and add it horizontally and vertically centre in UiView container.

enter image description here

and result will be as expected

Please check the following link to download xib file for collectionViewCell

Link - https://drive.google.com/a/erginus.com/file/d/0B2WiGVeE-REPakh3c21rWDlEWUU/view?usp=sharing
iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • Link-only answers are rarely useful. It's great that you've provided something the OP can download, but that's just a comment. For your answer, would be best to provide an actual explanation (to go with your excellent screenshot). – matt Aug 29 '17 at 05:20
  • okay let me edit the answer for full detail as you Said, thanks. so should I remove the link ? as I had used constraints which may not be easily configured by the user who had posted question. this is why I posted a link to let user download and see the answer – iOS Geek Aug 29 '17 at 05:25
  • As I said in my question: I know how to accomplish it using dummy view with constraints in IB. I'd like to make it done using UILayoutGuides. Nonetheless, thanks for your answer – theDC Aug 29 '17 at 06:04