gpt4 book ai didi

button - iOS自定义按钮类似于barbutton

转载 作者:可可西里 更新时间:2023-11-01 04:40:38 34 4
gpt4 key购买 nike

我想将按钮放置在具有动态文本的不同位置(而不是在导航栏中)。我希望它们看起来类似于黑色导航栏按钮项(具有灰色和黑色渐变)。

我将如何着手创建这些?它们需要是基于按钮文本的动态宽度。我知道我可以创建 png 文件并拉伸(stretch)它们,但有更好的方法吗?

最佳答案

您需要使用图像自行创建按钮。只需创建一个自定义 UIButton 并为您感兴趣的各种按钮状态分配适当的图像。

您可以使用 UIImagestretchableImageWithLeftCapWidth 方法从设计用于拉伸(stretch)的图像创建可拉伸(stretch)图像,并使用 NSString >sizeWithFont 方法来确定按钮的大小。

http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight :

像这样的东西可能会成功:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

int capHeight = 31;
int capWidth = 9;
UIImage *buttonTemplate = [UIImage imageNamed:@"button_template.png"];
UIImage *stretchedButtonImage = [buttonTemplate stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
[button setBackgroundImage:stretchedButtonImage forState:UIControlStateNormal];

关于button - iOS自定义按钮类似于barbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3765467/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com