iOS UIButton如何制作这样漂亮的按钮?

gee*_*eks 19 iphone background uibutton ios

这是UIButton吗?如果是,背景红色怎么办?显然,它不是红色UIColor?Xcode中是否包含相同的工具或实用程序以允许制作这样的按钮?

在此输入图像描述

djr*_*ero 15

您可以使用私有类生成类似的按钮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();
Run Code Online (Sandbox Code Playgroud)

获得png后,将其用作按钮背景.

或者,您可以通过编程方式构建按钮(由Jeff Lamarche提供).

  • @BradLarson该类未在应用程序中使用,只是为了创建PNG. (2认同)

Dav*_*dek 5

我写了几个类来为我写的计算器生成按钮.这些类根据标准颜色或RGB输入RGB值生成按钮.这些按钮位于Github上的BindleKit项目中:https://github.com/bindle/BindleKit

我对Change UIButton背景颜色的回答描述了如何使用类以及在何处查找源代码.

要查看按钮的示例,请在Github项目中编译BKCatalog应用程序或查看屏幕截图https://github.com/bindle/BindleKit/blob/master/screenshots/BKCatalog-BKButtons.png