iOS image stretching solution

Source: Internet
Author: User
Tags vars

UIButton to achieve background stretching, that is, there are two ways to stretch the middle of the picture without stretching:

The first approach is simple and more versatile. The idea is to directly stretch the image you want to setbackgroundimage, with the following code:

    1. UIImage *image = [UIImage imagenamed:@ "Image.png"];
    2. image = [Image Stretchableimagewithleftcapwidth:floorf (image. Size. width/2) Topcapheight:floorf  (Image. Size. height/2)];


After setting the left end cap, rightcapwidth = Image.size.width-(image.leftcapwidth + 1); That is, one pixel in the middle of the picture is used for stretching. Perpendicular to the same direction. After setting, the image can be automatically stretched regardless of what control it is placed in.

  1. UIImage *buttonimage = [UIImage imagenamed:@ "Contact.png"];
  2. Buttonimage = [Buttonimage Stretchableimagewithleftcapwidth:floorf (buttonimage. Size. width/2)   Topcapheight:floorf (buttonimage. Size. height/2)];
  3. UIImage *buttonimageselected = [UIImage imagenamed:@ "Contactselected.png"];
  4. Buttonimage = [Buttonimage Stretchableimagewithleftcapwidth:floorf (buttonimage. Size. width/2)   Topcapheight:floorf (buttonimage. Size. height/2)];
  5. UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
  6. Button. frame = CGRectMake (0, 0, 240, 44);
  7. [Button setbackgroundimage:buttonimage forstate:uicontrolstatenormal];
  8. [Button setbackgroundimage:buttonimageselected forstate:uicontrolstatehighlighted];
  9. Button. Center = cgpointmake (160, 240);
  10. [Button settitle:@ "button" forstate:uicontrolstatenormal];
  11. [Button settitle:@ "ButtonClick" forstate:uicontrolstatehighlighted];
  12. [self. View Addsubview:button];


The second method is to add a uiimageview to the UIButton, extrude the ImageView, and then set the button's background to Clearcolor, and so on. Put the ImageView into the button, and sendtoback, get the effect. The code is as follows:
Just ImageView stretched the code

  1. Uiimageview *strechtest = [[Uiimageyiview alloc] initwithimage:[uiimage imagenamed:@ "Contact.png"];
  2. [Strechtest Setcontentstretch:cgrectmake (0. 5f, 0. 5f, 0. F, 0. f)];
  3. CGRect frame = Strechtest. Frame;
  4. Frame. Size. Width + = 100;
  5. Strechtest. frame = frame;

  1. Put the ImageView into the button and set it to back
  2. UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
  3. Button1. frame = frame;
  4. Button1. Center = cgpointmake (160, 140);
  5. [Button1 addsubview:strechtest];
  6. [Button1 sendsubviewtoback:strechtest];
  7. [Button1 setbackgroundcolor:[uicolor Clearcolor]];
  8. [Button1 settitle:@ "button" forstate:uicontrolstatenormal];
  9. [Button1 settitle:@ "ButtonClick" forstate:uicontrolstatehighlighted];
  10. [self. View Addsubview:button];


Effect:

iOS image stretching solution

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.