现在App的底部栏、侧边栏、顶部栏经常出现一些包含图像和文字的Item,以前用按钮上面添加label和imageView,  想想实在是对资源的浪费。。

                

      图1 — 底部栏                   图2 — 侧边栏

  apple已经考虑到这点,UIButton里其实已经自带了label和imageView,虽然不能像设置frame一样设置它们的位置,

但是可以通过设置四个边的EdgeInset来“挤”。

  但是"挤"是个技术活,要使得图片和文字怪怪的听话可不是简单,一点点的调整很繁琐。

UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。

1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。

2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。

3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。

  需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置。居左时,运行结果如图1.

前提:UIButton: width=220, height=100,  image: width=height=36    text width=62

想要让图片和文本上下排列,需要让image向下偏移10(距离上边间隙),然后向右偏移92( button.width - image.width / 2),计算下来为 [btn setImageEdgeInsets:UIEdgeInsetsMake(5, 92, 0, 0)],它的偏移是针对它图1时的位置,如果想向上偏移传的为负值。

下面要计算机文本偏移了,向下偏移46 (36+10 图片的高度+间隙),向右偏移44 ( (button.width - text.width) / 2  -  image.width );因为文本本身起始x方向位置是从image.width开始的,所以算偏移时,要减掉这个宽度。计算结果为[btn setTitleEdgeInsets:UIEdgeInsetsMake(46, 44, 0, 0)];

图1

图2

  文字和图片的摆放一般有两种情况: 上下摆放和左右摆放;

  参考了stackoverflow:

  看看左右摆放:

             // the space between the image and text
CGFloat spacing = 6.0; // lower the text and push it left so it appears centered
// below the image
CGSize imageSize = button.imageView.frame.size;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0); // raise the image and push it right so it appears centered
// above the text
CGSize titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);

  

The following version contains changes to support iOS 7+ that have been recommended in comments below. I haven't tested this code myself, so I'm not sure how well it works or whether it would break if used under previous versions of iOS.

  根据底下的评论建议,下面的代码包含更改后支持iOS7以上版本。我自己还没有测试这代码,不知道是否能用,或者在iOS7以前的版本中会崩溃。

             // the space between the image and text
CGFloat spacing = 6.0; // lower the text and push it left so it appears centered
// below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0); // raise the image and push it right so it appears centered
// above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);

    

附注: 其实如果是底部栏(情况一)的换可以考虑使用UITabBarController,自定义barItem就可以了。

当时有个需求:从UIViewController(登陆)"push" 到UITabBarViewController,  由于UIViewController和UITabBarViewController是不同的类,

直接用navigationController push会到UITabBarViewController的navigationController里,这时候你就需要设置每一个UITabBarItem对应的UINavigationController。

push的代码:

     FirstViewController*  viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];

     SecondViewController*  viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"View1" image:Nil tag:];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"View2" image:Nil tag:]; UINavigationController *navigate, *navigate2; navigate = [[UINavigationController alloc] initWithRootViewController:viewController1];
navigate2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
navigate.tabBarItem = item;
navigate2.tabBarItem = item2;
UITabBarController *tabBar = [[UITabBarController alloc] init ]; tabBar.viewControllers = [NSArray arrayWithObjects:navigate,navigate2, nil];
[self.navigationController pushViewController:tabBar animated:YES];

  

  这样做对资源要求更高,所以stackoverflow上这个问题是被投反对票的,有谁有更好的方法请留言。

参考: http://stackoverflow.com/questions/28000336/uiviewcontroller-push-uitabbarcontroller-and-push-uivewcontroller

关于UIViewController和UINavigationController的关系可以参考 http://blog.csdn.net/jerryvon/article/details/7597481, 写的很明白。

原文:  http://www.cnblogs.com/A--G/p/5131401.html

参考链接: 1. http://stackoverflow.com/questions/2451223/uibutton-how-to-center-an-image-and-a-text-using-imageedgeinsets-and-titleedgei

         2.  http://stackoverflow.com/questions/4564621/aligning-text-and-image-on-uibutton-with-imageedgeinsets-and-titleedgeinsets

      3. http://blog.csdn.net/dfqin/article/details/37813591

UIButton 使用imageEdgeInsets和titleEdgeInsets属性的更多相关文章

  1. UIButton的imageEdgeInsets 和 titleEdgeInsets

    我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets ...

  2. UIButton的titleEdgeInsets属性和imageEdgeInsets属性实现图片文字按要求排列

    button可以设置 titleEdgeInsets属性和 imageEdgeInsets属性来调整其image和label相对位置,具体参考http://stackoverflow.com/ques ...

  3. iOS:UIView、UIControl、UIButton、UILabel简单的属性和方法常识

    常见属性和方法 一 .UIVIew 常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点 (以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸 ...

  4. iOS-UIButton-文字位置,字体大小,边角样式,button种类,点击事件,内容位置

    一. 设置button的文字居左,居中,居右 //设置button居左 button.contentHorizontalAlignment = UIControlContentHorizontalAl ...

  5. UIButton的titleEdgeInsets和imageEdgeInsets属性

    转:http://www.cnblogs.com/huichun/p/3419596.html uiButton控件上自带了一个uiLabel类型的子控件和一个uiImageView类型的子控件,如果 ...

  6. UIButton中的三个UIEdgeInsets属性

    接着昨天的 UIButton中的三个UIEdgeInsets属性 ,今天我们具体谈谈UIButton的contentEdgeInsets.titleEdgeInsets.imageEdgeInsets ...

  7. IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...

  8. [iOS]详解调整UIButton的title和image的位置

    UIButton的默认布局是:title在右,image在左; 很多时候我们需要的是title在左边,或者title在下面,这时就需要调整UIButton的TitleLabel和ImageView的位 ...

  9. UIButton中的**EdgeInsets是做什么用的?

    UIButton中的**EdgeInsets是做什么用的? UIEdgeInsetsMake Creates an edge inset for a button or view.An inset i ...

随机推荐

  1. Java如何将Exception.printStackTrace()转换为String输出

    package com.test1; import java.io.PrintWriter; import java.io.StringWriter; public class T010 { /** ...

  2. python 零散记录(五) import的几种方式 序列解包 条件和循环 强调getattr内建函数

    用import关键字导入模块的几种方式: #python是自解释的,不必多说,代码本身就是人可读的 import xxx from xxx import xxx from xxx import xx1 ...

  3. [CODEVS2055]集合划分

    对于从1到N(1<=N<=3)的连续整数集合,划分成两个子集合,使得每个集合的数字之和相等.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的:{3} ...

  4. [ZETCODE]wxWidgets教程六:事件处理

    本教程原文链接:http://zetcode.com/gui/wxwidgets/events/ 翻译:瓶哥 日期:2013年12月7号星期六 邮箱:414236069@qq.com 主页:http: ...

  5. oracle查看表锁及解锁

    --kill session语句 1 alter system kill SESSION '2171,60490'; --以下几个为相关表 1 2 3 4 5 6 7 SELECT * FROM v$ ...

  6. Ubuntu下Django初体验(二)——创建工程及应用

    一.工程目录详解 创建工程后得到如下目录: 1. manage.py 管理项目.创建数据库.启动服务器等.测试等. 查看子命令: python manage.py 启动服务器: python mana ...

  7. hdoj 2401 Baskets of Gold Coins

    Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. Android 开发60条技术经验总结(转)

    Android 开发60条技术经验总结: 1. 全部Activity可继承自BaseActivity,便于统一风格与处理公共事件,构建对话框统一构建器的建立,万一需要整体变动,一处修改到处有效. 2. ...

  9. (7/18)重学Standford_iOS7开发_视图、绘制、手势识别_课程笔记

    第七课: 1.View 一般来说,视图是一个构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,并在其中绘制及添加触控事件等. ①视图的层级关系 一个视图只能有一个父视图,可以有多个子视图 - ( - ...

  10. Contact Form 7邮件发送失败的解决办法

    一.contact form 7无法发送邮件的原因 对mail()函数的不支持. Contact Form 7表单提交失败在使用过程中会出现,归根结底原因在于wordpress主机问题,由于国 内很多 ...