iOS中UIButton的titleEdgeInsets和imageEdgeInsets

  默认状态下,UIButton的imageView和titleLabel之间没有间距,而且两个作为整体,居中显示。

设置button的titleEdgeInsets和imageEdgeInsets不是根据按钮的上下左右边距,而是根据titleLabel和imageView的当前位置决定的,

但是在测试的过程中又发现问题,比如

CGFloat topMargin = 50;
self.btnTest.imageEdgeInsets = UIEdgeInsetsMake(topMargin, 0, 0, 0);

本行代码运行后按钮的imageView的位置应该相对之前的位置向下移动50,但是实际的情况并不是这样的,经过多次测试(topMargin取多次值),button的imageView向下偏移(以imageView之前的状态)的量始终是 topMargin/2。

(这样好奇怪啊。。。。。。最后经过思考……^_^~,由于imageEdgeInsets和titleEdgeInsets不是相对于按钮边距的值,而是相对于原来的位置的偏移。但是UIEdgeInsetsMake的参数有四个,top,left,button和right,而控件不可能同时即向上移动又向下移动,或者即向左又向右移动)

以下面为例:

self.btnTest.imageEdgeInsets = UIEdgeInsetsMake(topMargin, leftMargin, buttomMargin, rightMargin);

此时imageView相对于原始的偏移为:

x = (leftMargin - rightMargin)  / 2;

y = (topMargin - buttomMargin) / 2;

(实测)。

 

posted @ 2015-11-22 21:50  _level_  阅读(608)  评论(0编辑  收藏  举报