Alejo Next Alejo Next - 4 months ago 23
iOS Question

The width a element in swift

Well, i need the size a element in swift. Bat this...

let screenSize: CGRect = UIScreen.mainScreen().bounds
print(grid.frame.size.width, screenSize.width)
// 560.0 414.0


In the storyboard

View
+ Grid
+ Constrains
+ Grid.leading = leadingMargin
+ Grid.bottom = Bottom Layout Guide.top
+ Grid.top = top
+ Grid.trailing = leadingMargin


How do you capture the width (real) the element for Grid?

Answer

Your question is very vague but here is what I see:

  1. Your constraints don't seem right in any case. Why does grid have trailing to leadingMargin? Shouldn't it be trailing to trailingMargin?
  2. When using autolayout, frames do not automatically have their correct size, try adding this code before accessing a frame dimensions:

    grid.setNeedsLayout()
    grid.layoutIfNeeded()
    

    This way, an autolayout pass is forced, allowing the frames to be set.