[英]Center image above UIButton title
I am have custom UIButton that is 1/4 in height/width of the view. 我有自定义的UIButton,视图的高度/宽度为1/4。 What I am trying to do is set an image above the button's title with both of them being centered in said button.
我想做的是在按钮标题上方设置一个图像,使它们两个都位于该按钮的中心。
Code: 码:
CGRect imageFrame= CGRectMake(0, 0, 80, 80);
UIEdgeInsets imageInset = UIEdgeInsetsMake(0, 0, 30, 0);
UIEdgeInsets titleInset = UIEdgeInsetsMake(80, 0, 0, 0);
btnSideSettings = [UIButton buttonWithType:UIButtonTypeCustom];
btnSideSettings.frame = settingsFrame;
[btnSideSettings setImage:[UIImage imageNamed:@"btnSideSettings"] forState:UIControlStateNormal];
btnSideSettings.imageView.frame = imageFrame;
btnSideSettings.imageEdgeInsets = imageInset;
btnSideSettings.titleEdgeInsets = titleInset;
btnSideSettings.titleLabel.textAlignment = NSTextAlignmentCenter;
[btnSideSettings setTitle:@"Settings" forState:UIControlStateNormal];
Result: 结果:
I've tried a few variants of the above code, mostly editing the insets. 我尝试了上述代码的一些变体,主要是编辑插图。 If I comment out adding the picture, the title will center just fine as well.
如果我注释掉添加图片,标题也将居中。 Appreciate any help.
感谢任何帮助。
可以尝试添加自己的子视图(图像视图和标签),然后对元素的大小和位置进行完全而明确的控制,而不必尝试在按钮上操作图像和文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.