Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我想做一个带有三角形图标的按钮_Ios_Objective C_Iphone_Uibutton - Fatal编程技术网

Ios 我想做一个带有三角形图标的按钮

Ios 我想做一个带有三角形图标的按钮,ios,objective-c,iphone,uibutton,Ios,Objective C,Iphone,Uibutton,我想制作一个带有三角形图标的按钮,就像iBooks应用程序样式一样。 按下按钮时,将显示列表屏幕。 然后选择一个列表,按钮的语句将是所选文本和一个三角形图标。图标始终位于右侧 [self.button setImage:[UIImage imageNamed:@"arrow-down"] forState:UIControlStateNormal]; CGFloat titleWidth = self.button.titleLabel.bounds.size.width; CGFloat i

我想制作一个带有三角形图标的按钮,就像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);
所以我试着制作一个示例应用程序

但是,图标的位置将绝对偏离。
如何安装在右侧始终是图标。

我通过对UIButton进行子类化并覆盖
setTitle:forState:
以添加一个空格,然后在unicode中添加一个向下箭头,很容易做到这一点。只需设置标题,箭头就会自动添加到正确的字体。它还将非常长的名称处理为“极端l…名称”▼"

如果你想要一个例子或者仅仅是一个完整的子类,我建议你使用下拉菜单。
免责声明:我做到了。

我通过对UIButton进行子类化并覆盖
setTitle:forState:
以添加空格,然后在unicode中添加向下箭头,很容易做到这一点。只需设置标题,箭头就会自动添加到正确的字体。它还可以处理非常长的名称,如“非常长…”。。。名称▼"

如果你想要一个例子或者仅仅是一个完整的子类,我建议你使用下拉菜单。
免责声明:我成功了。

您可以使用
UIBezierPath
绘制此图,将其放入
UIButton
子类的
drawRect
方法中

static const float kTriangleHeight = 8.0f;
static const float kTriangleWidth = 8.0f;

UIBezierPath *trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(self.frame.size.width - kTriangleWidth - 3.0f, (self.frame.size.height / 2) - kTriangleHeight / 2)];
[trianglePath addLineToPoint:CGPointMake(self.frame.size.width - kTriangleWidth - 3.0f, (self.frame.size.height / 2) + kTriangleHeight / 2)];
[trianglePath addLineToPoint:CGPointMake(self.frame.size.width - 3.0f, self.frame.size.height / 2)];
[trianglePath closePath];
[[UIColor blueColor] set];
[trianglePath fill];

您可以使用
UIBezierPath
来绘制它,将它放入
UIButton
子类的
drawRect
方法中

static const float kTriangleHeight = 8.0f;
static const float kTriangleWidth = 8.0f;

UIBezierPath *trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(self.frame.size.width - kTriangleWidth - 3.0f, (self.frame.size.height / 2) - kTriangleHeight / 2)];
[trianglePath addLineToPoint:CGPointMake(self.frame.size.width - kTriangleWidth - 3.0f, (self.frame.size.height / 2) + kTriangleHeight / 2)];
[trianglePath addLineToPoint:CGPointMake(self.frame.size.width - 3.0f, self.frame.size.height / 2)];
[trianglePath closePath];
[[UIColor blueColor] set];
[trianglePath fill];

customButton-使用imagecustomButton-使用Image我认为这个解决方案很好。但我也认为外观不一样。我怀疑“\u25BE”会是上图中的unicode三角形。(我个人喜欢较大的一个)我认为这个解决方案很好。但我也认为外观不一样。我怀疑“\u25BE”“将是上图中的unicode三角形。(我个人喜欢大一点的)我试过你的代码。但我无法消除“lineToPoint”的错误。它不是
addLineToPoint
lineToPoint
是一种
NSBezierPath
方法。感谢您指出@Rob,我从Cocoa项目中复制了此代码,但没有更改这些行。我为“UIBezierPath”编辑了answerNo visible@interface,声明了选择器“lineToPoint:”我尝试了您的代码。但我无法消除“lineToPoint”的错误。它不是
addLineToPoint
lineToPoint
是一种
NSBezierPath
方法。感谢您指出@Rob,我从Cocoa项目中复制了此代码,但没有更改这些行。我为“UIBezierPath”编辑了answerNo visible@interface,声明选择器“lineToPoint:”