Get size of a View in React Native

前端 未结 7 1859
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 15:56

Is it possible to get the size (width and height) of a certain view? For example, I have a view showing the progress:



        
7条回答
  •  眼角桃花
    2020-11-29 16:36

    As of React Native 0.4.2, View components have an onLayout prop. Pass in a function that takes an event object. The event's nativeEvent contains the view's layout.

     {
      var {x, y, width, height} = event.nativeEvent.layout;
    }} />
    

    The onLayout handler will also be invoked whenever the view is resized.

    The main caveat is that the onLayout handler is first invoked one frame after your component has mounted, so you may want to hide your UI until you have computed your layout.

提交回复
热议问题