【发布时间】:2011-10-28 15:25:44
【问题描述】:
这是一个 UIButton 吗?如果是,背景红色是怎么做的?显然,它不是 红色 UIColor? Xcode 中是否包含相同的工具或实用程序来允许制作按钮 像这样?
【问题讨论】:
标签: iphone ios background uibutton
这是一个 UIButton 吗?如果是,背景红色是怎么做的?显然,它不是 红色 UIColor? Xcode 中是否包含相同的工具或实用程序来允许制作按钮 像这样?
【问题讨论】:
标签: iphone ios background uibutton
您可以使用私有类UIGlassButton 生成类似的按钮。
我首先在@schwa 的http://pastie.org/830884 看到它。
在您的 iOS 模拟器中使用此代码运行应用以创建自定义按钮(或在设备中,保存到 $(APP)/Documents 并启用 iTunes 文件共享)。
Class theClass = NSClassFromString(@"UIGlassButton");
UIButton *theButton = [[[theClass alloc] initWithFrame:CGRectMake(10, 10, 120, 44)] autorelease];
// Customize the color
[theButton setValue:[UIColor colorWithHue:0.267 saturation:1.000 brightness:0.667 alpha:1.000] forKey:@"tintColor"];
//[theButton setTitle:@"Accept" forState:UIControlStateNormal];
[self.view addSubview:theButton];
UIGraphicsBeginImageContext(theButton.frame.size);
CGContextRef theContext = UIGraphicsGetCurrentContext();
[theButton.layer renderInContext:theContext];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *theData = UIImagePNGRepresentation(theImage);
[theData writeToFile:@"/Users/<# your user #>/Desktop/button.png" atomically:NO];
UIGraphicsEndImageContext();
获得 png 后,将其用作按钮背景。
或者,您也可以build the buttons programmatically(作者 Jeff Lamarche)。
【讨论】:
我编写了几个类来为我编写的计算器生成按钮。这些类根据标准颜色或 RGB 输入 RGB 值生成按钮。这些按钮位于我在 Github 上的 BindleKit 项目中:https://github.com/bindle/BindleKit
我对@987654322@ 的回答描述了如何使用这些类以及在哪里可以找到源代码。
要查看按钮示例,请在 Github 项目中编译 BKCatalog 应用程序或查看屏幕截图https://github.com/bindle/BindleKit/blob/master/screenshots/BKCatalog-BKButtons.png
【讨论】:
您需要为按钮创建背景图片。
然后将图片添加到按钮中:
- (void)setImage:(UIImage *)image forState:(UIControlState)state
对于可调整大小的按钮,请参见:
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
您可以使用图形转换器、Photoshop 等程序。
【讨论】:
在 iOS 5 下,你有 UIAppearance。
【讨论】:
【讨论】: