dambros dambros - 10 days ago 4
iOS Question

VFL expression not translating IB constraints as expected

I am new to iOS development and VFL is giving me a huge headache. I am trying to translate the following constraints into VFL for a simple view as a study case but my width is never inferred and unless I explicitly specify a width, the view never shows up.

enter image description here

H:|-50-[v0]-50-|
V:|-20-[v0(100)]


As I understand, these expressions should translate to the IB constraints in the image. What am I doing wrong here?

Entire constraints declaration:

scrollView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "H:|-50-[v0]-50-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": bigFrameUIView]))

scrollView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0(100)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": bigFrameUIView]))

Answer

For UIScrollView there several rules. You can check them out here

But in short, you have to:

  • specify only one subview for your scrollView and attach it to your scrollView sides, so scrollView will use it as content
  • set some constraints for size of your added contentView, so it can infer scrollView about contentSize
  • place your other elements inside contentView of your scrollView

This code will probably work, if you add this v0 to UIView

Comments