我希望,这是一个非常简单的问题,但是在下面的代码(没有弧和*.nibs的项目)之后,我如何适应UIButton的框架:
_fotosButton = [UIButton buttonWithType: UIButtonTypeCustom];
[_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_normal.png"] forState: UIControlStateNormal];
[_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_highlight.png"] forState: UIControlStateHighlighted];
[_fotosButton setTitle: NSLocalizedString(@"FOTOS", nil) forState: UIControlStateNormal];
[_fotosButton setTitleColor: RGBCOLOR(11.0f, 176.0f, 225.0f) forState: UIControlStateNormal];
[_fotosButton setTitleColor: [UIColor grayColor] forState: UIControlStateDisabled];
[_fotosButton.titleLabel setFont:TTSTYLEVAR(kinoFont10)];
[_fotosButton sizeToFit];
[self updateButtonInsets:_fotosButton];
[self addSubview: _fotosButton];
- (void) updateButtonInsets:(UIButton*) button {
CGSize imageSize = button.imageView.frame.size;
CGSize titleSize = button.titleLabel.frame.size;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0f, -imageSize.width, -imageSize.height, 0.0);
titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);}
这是我的代码的结果:

因此,我如何使框架(绿色区域)与内容(title+image)没有不必要的衬垫。我不能硬编码按钮的框架值,因为应用程序使用本地化。谢谢!
发布于 2013-11-28 10:28:09
我不认为这是一个好的解决方案,但我会张贴它。(如有任何意见,将不胜感激)
titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);
[button setSize:CGSizeMake(titleSize.width, titleSize.height + imageSize.height)];所以,我已经手动设置了按钮大小,这就是现在的样子

谢谢
https://stackoverflow.com/questions/20212708
复制相似问题