- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在底部显示带有文本(单行/多行)的按钮,并在顶部显示图像(必须看起来是圆形的);图片和文字必须在点击时突出显示。像这样:
这是 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, 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, 0.0, - titleSize.width);
希望这有帮助..
关于ios - 使用 AutoLayout 创建顶部图像和底部标签的 UIButton (UIButtonTypeCustom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30589199/
我有一个里面有图像的按钮,当我按下按钮时我想要一个弹出窗口。当按钮为空,内部没有图像时,一切正常,我现在添加了图像,按钮不再工作。按钮对应于 IBAction,并通过 IBOutlet 连接到 XIB
我一定忽略了一些完全明显的事情?但我的按钮正确显示其图像和大小,但我根本无法显示标题。 我做了一个非常简单的测试,当我这样做时标题甚至没有显示: CGRect frameBtn = CGRec
我正在使用自定义按钮类型,因为我需要触摸区域是非矩形的,所以我使用了一个名为 OBShapedButton 的类,这是我无耻地在网上找到的。但是,我还想为这些按钮着色以匹配玩家的颜色。 当谈到 UIB
我正在尝试在底部显示带有文本(单行/多行)的按钮,并在顶部显示图像(必须看起来是圆形的);图片和文字必须在点击时突出显示。像这样: 这是 viewDidLoad 中的代码: btArtAndPaint
我想在我的 View 中添加一个无边框的 UIButton。使用界面生成器,我通过从对象库中拖动一个 Round Rect Button 来完成此操作。然后,在属性检查器上,将类型更改为自定义并将标题
我想在单击时更改按钮颜色。我用过: [button setBackgroundColor:[UIColor redColor]]; 但这只在按钮的四个角上显示红色,而不是在整个按钮上显示红色,而且当我
我是一名优秀的程序员,十分优秀!