简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM