iOS-如何调整UIButton子视图的位置
2018-07-31 本文已影响83人
IMKel
以下代码是笔者再工作中总结得来的,如果错误,请多多指教!
给按钮创建个分类UIButton+EdgeInsets,然后将代码拷贝到对应文件即可
- UIButton+EdgeInsets.h文件
/**
图片在左,文字在右
@param space 图片和文字的间隔
*/
- (void)imageOnLeftWithSpace:(CGFloat)space;
/**
图片在右,文字在左
@param space 图片和文字的间隔
*/
- (void)imageOnRightWithSpace:(CGFloat)space;
/**
图片在上,文字在下
@param space 图片和文字的间隔
*/
- (void)imageOnTopWithSpace:(CGFloat)space;
/**
图片在下,文字在上
@param space 图片和文字的间隔
*/
- (void)imageOnBottomWithSpace:(CGFloat)space;
/**
用于有图片有文字的按钮。
功能:设置图片和文字之间的间距,并且可设置内容距离父视图左边的间距
@param space1 图片和文字之家的间距
@param space2 内容距离父视图左边的间距
*/
- (void)imageOnLeftWithSpace:(CGFloat)space1 contentLeftSpace:(CGFloat)space2;
- UIButton+EdgeInsets.m文件
- (void)imageOnLeftWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
self.titleEdgeInsets
= UIEdgeInsetsMake(
0,// top
space * 0.5,// left
0,// bottom
- space * 0.5);// right
self.imageEdgeInsets
= UIEdgeInsetsMake(
0,// top
- space * 0.5,// left
0,// bottom
space * 0.5);// right
}
- (void)imageOnRightWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
self.titleEdgeInsets
= UIEdgeInsetsMake(0,// top
- self.imageView.bounds.size.width - space * 0.5,// left
0,// bottom
self.imageView.bounds.size.width + space * 0.5);// right
self.imageEdgeInsets
= UIEdgeInsetsMake(0,// top
self.titleLabel.bounds.size.width + space * 0.5,// left
0,// bottom
- self.titleLabel.bounds.size.width - space * 0.5);// right
}
- (void)imageOnTopWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
BOOL holisticUpMove = self.titleLabel.bounds.size.height > self.imageView.bounds.size.height;
CGFloat offset = fabs(self.imageView.bounds.size.height - self.titleLabel.bounds.size.height) / 2;
CGFloat imageTop = 0;
CGFloat imageLeft = 0;
CGFloat imageBottom = 0;
CGFloat imageRight = 0;
if (holisticUpMove) {
// 整体上移
imageTop = -self.imageView.bounds.size.height * 0.5 - offset - space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = self.imageView.bounds.size.height * 0.5 + offset + space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}else {
// 整体下移
imageTop = -self.imageView.bounds.size.height * 0.5 + offset - space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = self.imageView.bounds.size.height * 0.5 - offset + space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}
self.imageEdgeInsets
= UIEdgeInsetsMake(
imageTop,// top
imageLeft,// left
imageBottom,// bottom
imageRight);// right
CGFloat labelTop = 0;
CGFloat labelLeft = 0;
CGFloat labelBottom = 0;
CGFloat labelRight = 0;
if (holisticUpMove) {
// 整体上移
labelTop = self.titleLabel.bounds.size.height * 0.5 - offset + space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = -self.titleLabel.bounds.size.height * 0.5 + offset - space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}else {
// 整体下移
labelTop = self.titleLabel.bounds.size.height * 0.5 + offset + space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = -self.titleLabel.bounds.size.height * 0.5 - offset - space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}
self.titleEdgeInsets
= UIEdgeInsetsMake(
labelTop,// top
labelLeft,// left
labelBottom,// bottom
labelRight);// right
}
- (void)imageOnBottomWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
BOOL holisticUpMove = self.imageView.bounds.size.height > self.titleLabel.bounds.size.height;
CGFloat offset = fabs(self.imageView.bounds.size.height - self.titleLabel.bounds.size.height) / 2;
CGFloat imageTop = 0;
CGFloat imageLeft = 0;
CGFloat imageBottom = 0;
CGFloat imageRight = 0;
if (holisticUpMove) {
// 整体需要上移
imageTop = self.imageView.bounds.size.height * 0.5 - offset + space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = - self.imageView.bounds.size.height * 0.5 + offset - space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}else {
// 整体需要下移
imageTop = self.imageView.bounds.size.height * 0.5 + offset + space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = - self.imageView.bounds.size.height * 0.5 - offset - space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}
self.imageEdgeInsets
= UIEdgeInsetsMake(
imageTop,// top
imageLeft,// left
imageBottom,// bottom
imageRight);// right
CGFloat labelTop = 0;
CGFloat labelLeft = 0;
CGFloat labelBottom = 0;
CGFloat labelRight = 0;
if (holisticUpMove) {
// 整体上移
labelTop = - self.titleLabel.bounds.size.height * 0.5 - offset - space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = self.titleLabel.bounds.size.height * 0.5 + offset + space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}else {
// 整体下移
labelTop = - self.titleLabel.bounds.size.height * 0.5 + offset - space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = self.titleLabel.bounds.size.height * 0.5 - offset + space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}
self.titleEdgeInsets
= UIEdgeInsetsMake(
labelTop,// top
labelLeft,// left
labelBottom,// bottom
labelRight);// right
}
- (void)imageOnLeftWithSpace:(CGFloat)space1 contentLeftSpace:(CGFloat)space2
{
[self imageOnLeftWithSpace:space1];
CGFloat insetLeft = self.imageView.frame.origin.x - space2;
self.contentEdgeInsets = UIEdgeInsetsMake(0, - insetLeft, 0, insetLeft);
}