SizeToFit usage and usage, sizetofit usage

Source: Internet
Author: User

SizeToFit usage and usage, sizetofit usage

I have recently encountered sizeToFit, so I checked some information.

In the official documentation

-(Void) sizeToFit;

// Callsizethatfits: with current view bounds and changes bounds size. Call this method to change the size of the current view (Adaptive size)

  

Now I load an image from the webpage and use the sizeToFit Method

// Set an image for imageView

-(Void) setImage {

// Obtain the image URL from the webpage

NSURL * url = [NSURL URLWithString: @ "http://img.taopic.com/uploads/allimg/140403/240438-1404030TT089.jpg"];

// Obtain data based on the url

NSData * data = [NSData dataWithContentsOfURL: url];

// Obtain the Image Based on data

UIImage * image = [UIImage imageWithData: data];

// Set the image to imageView

Self. imageView. image = image;

// Call the sizeTofit method to obtain the size suitable for the current view

[Self. imageView sizeToFit];

// Manually set frame

// Self. imageView. frame = CGRectMake (0, 0,375,667 );

NSLog (@ "sizeToFit ----- % @", NSStringFromCGRect (self. imageView. frame ));

// Set the scrollView's scroll range

Self. scrollView. contentSize = image. size;

}

Print the imageView size.

 

22:33:39. 401TestSizeToFit [20263: 1497379] sizeToFit ----- {0, 0}, {1000,564 }}

 

We found that the imageView size is exactly the same as the image size without setting the imageView, so we can say that sizeToFit is to let the current View adjust its own size based on the size of the child element (the most suitable ).

@ Interface UIView (UIViewGeometry)

UILabel can also call this method to adjust the size of UILable based on the text because it is a classification of UIView (Similarly, other controls can also be used );

Although UIView and its sub-controls can be used, sizeToFit is not suitable for manual layout.

Therefore, we generally do not use it in actual development, but we use sizeToFit in some scenarios where the system is automatically deployed.

1. Set navigationItem,

2. UIBarButtonItem settings

3. Add text to UILabel and adjust the label size to adapt to the text. We also call the sizeToFit method.

4. etc.

 

PS: I found the information when I reported an error when loading images from a webpage.

The App Transport Security (ATS) feature is added in iOS9, which mainly enables the HTTP used in the original request to be transferred to the TLS1.2 protocol. This also means that all HTTP protocols are forced to use HTTPS for transmission.

 

Therefore, we need to add an attribute to the info. plist file.

<Key> NSAppTransportSecurity </key>

<Dict>

<Key> NSAllowsArbitraryLoads </key>

<True/>

</Dict>

 

 

 

 

  

 

 

            

Related Article

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.