- (CGSize)sizeThatFits:(CGSize)size; // return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (void)sizeToFit; // calls sizeThatFits: with current view bounds and changes bounds size.
sizeThatFits:
Asks the view to calculate and return the size that best fits its subviews.
Parameters
-
size
-
The current size of the receiver.
Return Value
A new size that fits the receiver’s subviews.
Discussion
The default implementation of this method returns the size portion of the view’s bounds rectangle. Subclasses can override this method to return a custom value based on the desired layout of any subviews. For example, a UISwitch object returns a fixed size value that represents the standard size of a switch view, and a UIImageView object returns the size of the image it is currently displaying.
This method does not resize the receiver.
Availability
- Available in iOS 2.0 and later.
sizeToFit
Resizes and moves the receiver view so it just encloses its subviews.
Discussion
Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.
You should not override this method. If you want to change the default sizing information for your view, override the sizeThatFits: instead. That method performs any needed calculations and returns them to this method, which then makes the change.
Availability
- Available in iOS 2.0 and later.
本文详细介绍了iOS中视图大小适配的方法,包括如何使用sizeThatFits:方法来计算并返回最适合子视图的大小,以及如何通过sizeToFit方法调整视图大小以完全覆盖子视图。
249

被折叠的 条评论
为什么被折叠?



