Câu hỏi phỏng vấn iOS
Câu hỏi

What's the difference between the frame and the bounds ?

Câu trả lời

The difference between the frame and the bounds of a UIView in iOS development is a fundamental concept that relates to how views are positioned and sized within their parent views and their own coordinate systems.

Frame

  • Definition: The frame of a UIView is a rectangle that defines the view's location and size in the coordinate system of its superview.
  • Components: It consists of an origin (x, y) and a size (width, height).
  • Usage: The frame is used to position the view within its parent view. For example, if a view is placed 100 points from the left and 100 points from the top of its superview, its frame might be {100, 100, 200, 200} for a 200x200 view.
  • Transformations: When a view is transformed (e.g., rotated, scaled), the frame changes to reflect the new position and size relative to the superview.

Bounds

  • Definition: The bounds of a UIView is a rectangle that defines the view's location and size in its own coordinate system.
  • Components: It also consists of an origin (x, y) and a size (width, height), but the origin is typically (0, 0).
  • Usage: The bounds is used to define the internal coordinate system of the view. It is particularly useful for drawing and laying out subviews within the view.
  • Transformations: The bounds remains constant even when the view is transformed. The size of the bounds does not change with transformations, but the content within the view may be affected.

Key Differences

  1. Coordinate System:

    • Frame: Relative to the superview's coordinate system.
    • Bounds: Relative to the view's own coordinate system.
  2. Origin:

    • Frame: The origin can be any point within the superview's coordinate system.
    • Bounds: The origin is typically (0, 0), but it can be adjusted for scrolling or other purposes.
  3. Transformations:

    • Frame: Changes with transformations (e.g., rotation, scaling).
    • Bounds: Remains constant; transformations do not affect the bounds.

Practical Example

Consider a view viewA that contains a subview viewB. If viewA has a frame of {50, 50, 200, 200}, it means viewA is positioned 50 points from the left and 50 points from the top of its superview, and it is 200x200 in size. If viewA's bounds are {0, 0, 200, 200}, it means the internal coordinate system of viewA starts at (0, 0) and extends to 200x200.

If viewA is rot...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What's your preference when writing UI's? Xib files, Storyboards or programmatic UIView ?

middle

What considerations do you need when writing a UITableViewController which shows images downloaded from a remote server?

middle

What are some pros and cons of Core Data?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào