ios – 创建UIButton(UIButtonTypeCustom),顶部带有图像,底部带有AutoLayout标签

ios – 创建UIButton(UIButtonTypeCustom),顶部带有图像,底部带有AutoLayout标签,第1张

概述我试图在它的底部显示带有文本(单/多行)的按钮.顶部的图像(必须看起来是圆形的);图像&单击时必须突出显示文本.像这样: 这是viewDidLoad中的代码: btArtAndPainting = [UIButton buttonWithType:UIButtonTypeCustom];btArtAndPainting.backgroundColor = [[UIColor cyanColor] 我试图在它的底部显示带有文本(单/多行)的按钮.顶部的图像(必须看起来是圆形的);图像&单击时必须突出显示文本.像这样:

这是vIEwDIDLoad中的代码:

btArtAndPainting = [UIbutton buttonWithType:UIbuttonTypeCustom];btArtAndPainting.backgroundcolor = [[UIcolor cyancolor] colorWithAlphaComponent:1.00f];btArtAndPainting.translatesautoresizingMaskIntoConstraints = NO;self.vwContainer.translatesautoresizingMaskIntoConstraints = NO;[self.vwContainer addSubvIEw:btArtAndPainting];[btArtAndPainting setimage:[UIImage imagenamed:kimgcategoryArtAndPaintingnormal] forState:UIControlStatenormal];[btArtAndPainting setimage:[UIImage imagenamed:kimgcategoryArtAndPaintingHighlighted] forState:UIControlStateHighlighted];[btArtAndPainting setTitle:@"Art and Painting" forState:UIControlStatenormal];[btArtAndPainting setTitle:@"Art and Painting" forState:UIControlStateHighlighted];btArtAndPainting.tintcolor = [UIcolor redcolor];btArtAndPainting.TitleLabel.Font = [UIFont systemFontOfSize:10.00f];btArtAndPainting.TitleLabel.textcolor = [UIcolor whitecolor];btArtAndPainting.TitleLabel.textAlignment = NSTextAlignmentCenter;btArtAndPainting.TitleLabel.lineBreakMode = NSlineBreakByWorDWrapPing | NSlineBreakByTruncatingTail;btArtAndPainting.TitleLabel.numberOflines = 0;btArtAndPainting.imageEdgeInsets = UIEdgeInsetsMake(0.00f,8.00f,20.00f,8.00f);btArtAndPainting.TitleEdgeInsets = UIEdgeInsetsMake(32.00f,0.00f,0.00f);NSArray *constraintsWIDth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btArtAndPainting(60)]|"                                                                     options:0                                                                     metrics:nil                                                                       vIEws:vIEwDictionary];NSArray *constraintsHeight = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchbar]-10-[btArtAndPainting(60)]"                                                                    options:0                                                                    metrics:nil                                                                      vIEws:vIEwDictionary];[self.vwContainer addConstraints:constraintsHeight];[self.vwContainer addConstraints:constraintsWIDth];[btArtAndPainting layoutIfNeeded];[btArtAndPainting needsUpdateConstraints];

问题是文本根本不显示(在正常和突出显示的状态下).请参阅下面的链接图像.
如何使用UIEdgeInset为文本腾出空间?我也试过创建一个UIbutton类,但效果大致相同.

解决方法 请使用以下代码设置插图,
CGfloat spacing = 1.0;CGSize imageSize = btArtAndPainting.imageVIEw.frame.size;btArtAndPainting.TitleEdgeInsets = UIEdgeInsetsMake(0.0,- imageSize.wIDth,- (imageSize.height + spacing),0.0);CGSize TitleSize = btArtAndPainting.TitleLabel.frame.size;btArtAndPainting.imageEdgeInsets = UIEdgeInsetsMake(- (TitleSize.height + spacing),0.0,- TitleSize.wIDth);

希望这可以帮助..

总结

以上是内存溢出为你收集整理的ios – 创建UIButton(UIButtonTypeCustom),顶部带有图像,底部带有AutoLayout标签全部内容,希望文章能够帮你解决ios – 创建UIButton(UIButtonTypeCustom),顶部带有图像,底部带有AutoLayout标签所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/web/1100332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存