ios 图片拉伸

2017-11-21  本文已影响2人  天空的羁绊
原图: 31.png

效果:


屏幕快照 2017-11-21 下午6.30.53.png
    UIImage *buttonNorma = [UIImage imageNamed:@"31"];
    // 进行宽度上的延伸,如果要进行高度上的延伸,改变第一个和第三个参数的值,此方法是保证图片边缘的框架的前提下对图片中间进行高度或者宽度的延伸
    UIImage *stretchNormal = [buttonNorma resizableImageWithCapInsets:UIEdgeInsetsMake(23, 8, 22, 15)];
    
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"拉伸图片" forState:UIControlStateNormal];
    [button1 setBackgroundImage:stretchNormal forState:UIControlStateNormal];
    button1.frame = CGRectMake(30, 400, 200, 46*2);
    button1.layer.cornerRadius = 5;
    [self.view addSubview:button1];
    
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"直接设置图片" forState:UIControlStateNormal];
    [button2 setBackgroundImage:buttonNorma forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 500, 200, 46*2);
    button2.layer.cornerRadius = 5;
    [self.view addSubview:button2];

这里是另一个人对拉伸方法的一种解析,不懂的可以看下,虽然效果一样,但我对其理解并不认同,很多方法直接查看API即可理解。
http://www.jianshu.com/p/a577023677c1

上一篇下一篇

猜你喜欢

热点阅读