iOS开发 设置button的image的位置

iOS开发 设置button的image的位置,第1张

开发过程中经常遇到通过imageEdgeInsets和titleEdgeInsets去设置button中的image,title的位置,来达到想要的效果。但因为对其原理的不了解,经常碰壁,设置了却达不到自己想要的效果。终于找到这篇文章,文章作者解析的很详细,这里记录一下,方便自己日后查阅。

1.常用的button样式,图片在上,文字在下

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem]

leftBtn.frame=CGRectMake(0,0,25,25)

[leftBtnsetBackgroundImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal]

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked:)forControlEvents:UIControlEventTouchUpInside]

UIBarButtonItem* leftBarBtn = [[UIBarButtonItemalloc]initWithCustomView:leftBtn]

UIBarButtonItem* spaceItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil]

spaceItem.width= -15

self.navigationItem.leftBarButtonItems=@[spaceItem,leftBarBtn]


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/7848535.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存