一、内边距

UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距

1、contentEdgeInsets

  如果是设置contentEdgeInsets, 会把UIImageView和UIlabel当做一个整体移动

btn.contentEdgeInsets = UIEdgeInsetsMake(, , , );

对应状态:

2、titleEdgeInsets/imageEdgeInsets

  如果是设置titleEdgeInsets/imageEdgeInsets. 那么不会影响到另外一个, 也就是只会改变当前设置的这个控件

    btn.titleEdgeInsets = UIEdgeInsetsMake(, , , );

    btn.imageEdgeInsets = UIEdgeInsetsMake(30 ,0 , , );

二、图片拉伸

1.iOS5以前

UIButton *btn = [[UIButton alloc] init];

    UIImage *image = [UIImage imageNamed:@"common_button_blue_highlighted"];

    // LeftCapWidth: 左边多少不能拉伸

    // 右边多少不能拉伸 = 控件的宽度 - 左边多少不能拉伸 - 1

    //  right  =  width - leftCapWidth - 1

    // 1 = width - leftCapWidth - right

    // topCapHeight: 顶部多少不能拉伸

    // 底部有多少不能拉伸 = 控件的高度 - 顶部多少不能拉伸 - 1

    //  bottom =  height - topCapWidth - 1

    // 1 = height - topCapWidth - bottom

    UIImage *newImage = [image stretchableImageWithLeftCapWidth: topCapHeight:];

2.iOS5开始

    // UIEdgeInsets是告诉系统哪些地方需要受保护, 也就是不可以拉伸

    // resizableImageWithCapInsets默认拉伸方式是平铺

    UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5);

    UIImage *newImage =  [image resizableImageWithCapInsets:insets];

3.iOS6开始

  // resizingMode指定拉伸模式

    // 平铺

    // 拉伸

    UIEdgeInsets insets = UIEdgeInsetsMake(, , , );

    UIImage *newImage =  [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

    [btn setBackgroundImage:newImage forState:UIControlStateNormal];

    btn.frame = CGRectMake(, , , );

    [self.view addSubview:btn];

iOS开发——UI基础-按钮内边距,图片拉伸的更多相关文章

  1. iOS开发——UI基础-按钮的创建和设置

    @interface ViewController () - (IBAction)customBtnClick; @end @implementation ViewController - (void ...

  2. iOS开发UI基础—手写控件,frame,center和bounds属性

    iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

  3. iOS开发UI篇—Quartz2D使用(图片剪切)

    iOS开发UI篇—Quartz2D使用(图片剪切) 一.使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - ...

  4. IOS开发UI基础--数据刷新

    IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...

  5. iOS开发——UI基础-UIButton、UIImageView、UILabel的选择

    1.UILabel - UILabel的常见属性 @property(nonatomic,copy) NSString *text; 显示的文字 @property(nonatomic,retain) ...

  6. IOS开发UI基础之UIButton

    什么是按钮?

  7. iOS开发-UI基础Demo

    现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...

  8. IOS开发UI基础UITableView的属性

    UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...

  9. iOS开发——UI基础-控制器,IBAction和IBOutlet,UIView

    第一个ios程序 @interface ViewController : UIViewController @property(nonatomic, weak)IBOutlet UILabel *la ...

随机推荐

  1. Yocto开发笔记之《嵌入式linux libcurl编程》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...

  2. 《Struts2.x权威指南》学习笔记2

    在学习了第二章后,我想要将struts分类,修改一下struts.xml的默认读取路径如下图. 在IntelliJ中,resources是struts的默认路径 修改路径,需要在web.xml中添加s ...

  3. JavaScript学习笔记——节点

    javascript-节点属性详解 根据 DOM,HTML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 HTML 标签是一个元素节点 包含在 HTML 元素中 ...

  4. HTML学习笔记——CSS初涉

    1>嵌入式css写法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  5. JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession

    会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...

  6. 11 Clever Methods of Overfitting and how to avoid them

    11 Clever Methods of Overfitting and how to avoid them Overfitting is the bane of Data Science in th ...

  7. python 传递结构体指针到 c++ dll

    CMakeLists.txt # project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED ...

  8. Unity State Machine

    https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours http: ...

  9. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  10. Drupal 7.31SQL注入getshell漏洞利用详解及EXP

    0x00 这个漏洞威力确实很大,而且Drupal用的也比较多,使用Fuzzing跑字典应该可以扫出很多漏洞主机,但是做批量可能会对对方网站造成很大的损失,所以我也就只是写个Exp不再深入下去. 0x0 ...