iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 10
0
自我挑戰組

iOS 筆記系列 第 10

frame vs bounds 系列 part 2:什麼時候使用

  • 分享至 

  • xImage
  •  

什麼時候要使用 frame,什麼時候要使用 bounds 呢?

蘋果官方文件View Programming Guide for iOS有說明:

You use the center and frame properties primarily for manipulating the geometry of the current view. For example, you use these properties when building your view hierarchy or changing the position or size of a view at runtime. If you are changing only the position of the view (and not its size), the center property is the preferred way to do so. The value in the center property is always valid, even if scaling or rotation factors have been added to the view’s transform. The same is not true for the value in the frame property, which is considered invalid if the view’s transform is not equal to the identity transform.

You use the bounds property primarily during drawing. The bounds rectangle is expressed in the view’s own local coordinate system. The default origin of this rectangle is (0, 0) and its size matches the size of the frame rectangle. Anything you draw inside this rectangle is part of the view’s visible content. If you change the origin of the bounds rectangle, anything you draw inside the new rectangle becomes part of the view’s visible content.

結論

frame 用在:

  • resizing view
  • moving / reposition view

bounds 用在:

  • when you do drawing inside the view with bounds e.g. in the drawRect: method of UIView.
  • adding subViews to a parent views bounds

來源:http://stackoverflow.com/a/13069568/3295047


上一篇
super 系列 part 4:系統內建方法要不要寫?
下一篇
檢查每個 function 跑了多久
系列文
iOS 筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言