我想制作一个带有三角形图标的按钮,如iBooks应用风格。
按下按钮后,将显示列表屏幕。然后选择一个列表,该按钮的语句将为所选文本和一个三角形图标。图标始终在右侧。
[self.button setImage:[UIImage imageNamed:@"arrow-down"] forState:UIControlStateNormal];
CGFloat titleWidth = self.button.titleLabel.bounds.size.width;
CGFloat imageWidth = self.button.imageView.bounds.size.width;
// Title is left Image is right
self.button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
self.button.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth, 0, -titleWidth);
因此,我尝试制作一个示例应用程序。
https://github.com/kawai-hiroyuki/LikeIBooksDropDownMenu
但是,图标的位置将完全偏离。我将如何做总是被安装在右边的图标。
我通过子类化UIButton并重写setTitle:forState:
以在unicode中添加一个空格和一个向下箭头来非常容易地做到这一点。只需设置标题,箭头就会自动以正确的字体添加。它还处理极长的名称,例如“ Extremely ... name▼”
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
title = [title stringByAppendingString:@" \u25BE"]; //Unicode - Small down arrow
[super setTitle:title forState:state];
[self sizeToFit];
}
如果您想要一个示例,或者只是一个完整的子类(也是一个下拉菜单),我建议您这样做。免责声明:我做到了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句