Tips for sharing the SizeToFit () method in iOS

Source: Internet
Author: User

The first time to write technical blog, did not write, mainly because they think that iOS technology has not studied so deep, afraid to write to fraught. The second one, is also lazy. In order to urge themselves, but also in order to and more iOS development enthusiasts to communicate and common progress, absolutely what will be the experience, would share. At the same time, I hope that everyone will point out my mistakes and incomplete places.

OK, then we'll get to the point:

SizeToFit () Usage experience:

A lot of beginners, including me, at the time of learning, especially when the struggle with this sizetofit (). Here I will share some of my experience.

First, let's take a look at the official document statement to SizeToFit (): (Swift version)

Calls Sizethatfits:with current view bounds and changes bounds size.

Translation: Call the Sizethatfits method: parameter (the bounds of the current view and the size of the change).

public func sizetofit ()

Return ' best ' size to fit given size. Does not actually resize view. Default is return existing view size

Translate: Returns the ' best ' size to fit the given size. The view is not actually adjusted. The default value is to return the existing view size

public func sizethatfits (size: cgsize), cgsize

As we can see from the official documentation, when calling SizeToFit, it is similar to what the system will do to help me layout a size that it deems most appropriate.

In addition SizeToFit () is declared in UIView, so all of our view controls can call this method

Two. Since all the controls can call this method, but in the actual development, we do not seem to use it often, to do some layout what?

Reason: We generally call the SizeToFit method when the manual layout is not convenient, the following we will look specifically at which situations, not suitable for manual layout:

1.navigationBar Settings for Navigationitem (add two views above the control to item)

2. Settings for Uibarbuttonitem in toolbar (generally we also add a spring control)

Above two kinds of occasions can use SizeToFit this method, let the system give us to do automatic layout. (Note: If you add a control, we can set the Fram directly)

3. In Tabbar We cannot manually add the child controls, because Tabbar is automatically added according to the controller system by default Tabbaritem. (Guess the system may also automatically call this method)

Add text to 4.UILabel, then adjust the label size to fit the text, we also call the SizeToFit method

The above is only a personal experience and conjecture, welcome to correct and criticize

Tips for sharing the SizeToFit () method in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.