iOS視圖邊框的簡單做法,ios視圖邊框

來源:互聯網
上載者:User

iOS視圖邊框的簡單做法,ios視圖邊框

我們繪製UI介面的時候,一般我們做邊框是用layer,然後再給它上面添加陰影什麼的,我比較喜歡用下面這個方法,

UI弄幾張邊框的圖片,用代碼給圖片展開

- (UIImage *)changeBorderImage:(UIImage *)borderImage {    UIImage *image = borderImage;    CGFloat top = 2; // 頂端蓋高度    CGFloat bottom = 2 ; // 底端蓋高度    CGFloat left = 2; // 左端蓋寬度    CGFloat right = 2; // 右端蓋寬度    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);    // 指定為展開模式,伸縮後重新賦值    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];    return image;}

自訂一個邊框視圖或者在你的視圖下面添加一個帶有展開後的UIImage的UIImageView就行了

全部代碼:

BorderView.h

////  BorderView.h//  border_creat////  Created by 譚啟宏 on 15/12/15.//  Copyright © 2015年 譚啟宏. All rights reserved.//#import <UIKit/UIKit.h>@interface BorderView : UIImageView@property (nonatomic,strong)UIImage *borderImage;- (instancetype)initWithFrame:(CGRect)frame borderImage:(UIImage *)borderImage;@end

BorderView.m

////  BorderView.m//  border_creat////  Created by 譚啟宏 on 15/12/15.//  Copyright © 2015年 譚啟宏. All rights reserved.//#import "BorderView.h"@implementation BorderView- (instancetype)initWithFrame:(CGRect)frame borderImage:(UIImage *)borderImage {    self  =[super initWithFrame:frame];    if (self) {        self.image = [self changeBorderImage:borderImage];            }    return self;}- (void)setBorderImage:(UIImage *)borderImage {    self.borderImage = [self changeBorderImage:borderImage];}- (UIImage *)changeBorderImage:(UIImage *)borderImage {    UIImage *image = borderImage;    CGFloat top = 2; // 頂端蓋高度    CGFloat bottom = 2 ; // 底端蓋高度    CGFloat left = 2; // 左端蓋寬度    CGFloat right = 2; // 右端蓋寬度    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);    // 指定為展開模式,伸縮後重新賦值    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];    return image;}@end

ViewController.m

- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    BorderView *borderView = [[BorderView alloc]initWithFrame:CGRectMake(100, 100, 100, 50) borderImage:[UIImage imageNamed:@"tool_biankuang2"]];    [self.view addSubview:borderView];            BorderView *borderView1 = [[BorderView alloc]initWithFrame:CGRectMake(100, 200, 150, 100) borderImage:[UIImage imageNamed:@"tool_biankuang2"]];    [self.view addSubview:borderView1];        BorderView *borderView2 = [[BorderView alloc]initWithFrame:CGRectMake(100, 350, 30, 100) borderImage:[UIImage imageNamed:@"tool_biankuang"]];    [self.view addSubview:borderView2];    }

:

demo地址:http://pan.baidu.com/s/1dEpedBr

tips:圖片只需要上下左右距離邊部有幾象數,中間空一部分就行了

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.