A simple approach to the iOS view border

Source: Internet
Author: User
Tags uikit

When we draw the UI interface, we usually make the border with a layer and then add a shadow to it, I prefer to use the following method,

UI to get a few frames of picture, with code to stretch the picture

-(UIImage *) Changeborderimage: (UIImage *) borderimage {UIImage*image =Borderimage; CGFloat Top=2;//Top Cover HeightCGFloat bottom =2;//Bottom Cover HeightCGFloat left =2;//Left cover widthCGFloat right =2;//right End cover widthUiedgeinsets insets =Uiedgeinsetsmake (top, left, bottom, right); //specified as stretch mode, re-assigned after scalingImage =[Image Resizableimagewithcapinsets:insets Resizingmode:uiimageresizingmodestretch]; returnimage;}

Customize a border View or add a uiimageview with stretched uiimage underneath your view.

All code:

BorderView.h

// //   BorderView.h//  border_creat////  Created by Tan Qihong on 15/12/15. //   copyright©2015 year Tan Qihong. All rights reserved. // #import <UIKit/UIKit.h>@interface*borderimage; -(Instancetype) initWithFrame: (CGRect) frame borderimage: (UIImage *) borderimage; @end

borderview.m

////borderview.m//border_creat////Created by Tan Qihong on 15/12/15.//copyright©2015 year Tan Qihong. All rights reserved.//#import "BorderView.h"@implementationBorderview-(Instancetype) initWithFrame: (CGRect) frame borderimage: (UIImage *) borderimage { self=[Super Initwithframe:frame]; if(self) {self.image=[self changeborderimage:borderimage]; }    returnSelf ;}- (void) Setborderimage: (UIImage *) borderimage {self.borderimage=[self changeborderimage:borderimage];}-(UIImage *) Changeborderimage: (UIImage *) borderimage {UIImage*image =Borderimage; CGFloat Top=2;//Top Cover HeightCGFloat bottom =2;//Bottom Cover HeightCGFloat left =2;//Left cover widthCGFloat right =2;//right End cover widthUiedgeinsets insets =Uiedgeinsetsmake (top, left, bottom, right); //specified as stretch mode, re-assigned after scalingImage =[Image Resizableimagewithcapinsets:insets Resizingmode:uiimageresizingmodestretch]; returnimage;}@end

Viewcontroller.m

- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; Borderview*borderview = [[Borderview alloc]initwithframe:cgrectmake ( -, -, -, -) Borderimage:[uiimage imagenamed:@"tool_biankuang2"]];            [Self.view Addsubview:borderview]; Borderview*borderview1 = [[Borderview alloc]initwithframe:cgrectmake ( -, $, Max, -) Borderimage:[uiimage imagenamed:@"tool_biankuang2"]];        [Self.view Addsubview:borderview1]; Borderview*borderview2 = [[Borderview alloc]initwithframe:cgrectmake ( -, -, -, -) Borderimage:[uiimage imagenamed:@"Tool_biankuang"]];    [Self.view Addsubview:borderview2]; }

:

A simple approach to the iOS view border

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.