Iphone 如何在iOS 7中设计全圆形按钮?
我正在为iOS 7创建一个应用程序,我想要一个像呼叫键盘一样的全圆形按钮。Iphone 如何在iOS 7中设计全圆形按钮?,iphone,objective-c,ios7,uibutton,Iphone,Objective C,Ios7,Uibutton,我正在为iOS 7创建一个应用程序,我想要一个像呼叫键盘一样的全圆形按钮。 有人知道我们如何使iOS 7的按钮完全圆整,也可以为圆整按钮设置图像吗?//在.h文件中支持框架 **#import <QuartzCore/QuartzCore.h>** UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"imagename.png"
有人知道我们如何使iOS 7的按钮完全圆整,也可以为圆整按钮设置图像吗?//在.h文件中支持框架
**#import <QuartzCore/QuartzCore.h>**
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"imagename.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
**#导入**
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
[按钮设置图像:[UIImage imagename:@“imagename.png”]用于状态:UIControlStateNormal];
[按钮添加目标:自我操作:@选择器(单击)用于控制事件:UIControlEventTouchUpInside];
[按钮设置标题:@“显示视图”状态:uicontrol状态正常];
button.frame=CGRectMake(135.0,180.0,40.0,40.0)//宽度和高度应为相同的值
button.clipstobunds=是;
button.layer.cornerRadius=20//一半宽度
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:按钮];
为此,您需要使用QuartzCore/QuartzCore框架。这里有一个很好的教程看看这个有几种方法。例如,您可以对按钮图像使用UIImageView,对其使用圆角
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 33, 33)];
avatar.contentMode = UIViewContentModeScaleAspectFill;
avatar.layer.cornerRadius = 16;
avatar.layer.borderColor = [UIColor whiteColor].CGColor;
avatar.layer.borderWidth = 1;
avatar.clipsToBounds = YES;
是的,很简单。您需要添加quartzcore框架,并且按钮的框架必须是正方形,宽度和高度相等
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = myButton.frame.size.height/2.0;
myButton.clipsToBounds = YES;
myButton.layer.borderWidth = 1.0;
myButton.layer.borderColor = [UIColor blueColor].CGColor;
myButton.layer.backgroundColor =[UIColor cyanColor].CGColor;
myButton.backgroundColor = nil;
或者你可以这样做:
[myButton setImage:[self generateButtonImageOfSize:myButton.frame.size] forState:UIControlStateNormal];
-(UIImage *)generateButtonImageOfSize:(CGSize )buttonSize{
UIGraphicsBeginImageContextWithOptions(buttonSize, NO , 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGRect br = CGRectMake(0, 0, buttonSize.width, buttonSize.height);
CGContextFillEllipseInRect(context, br);
CGContextStrokeEllipseInRect(context, br);
//theres a pretty circle, wanna draw other stuff? do it here..
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
发布一些你尝试过的代码?哈哈,当我开始键入时,并没有答案,问题是如何在按钮中设置图像,所以请更新你的answer@Rushabh乌尔斯也不错answer@Jef+1很好的解释
[myButton setImage:[self generateButtonImageOfSize:myButton.frame.size] forState:UIControlStateNormal];
-(UIImage *)generateButtonImageOfSize:(CGSize )buttonSize{
UIGraphicsBeginImageContextWithOptions(buttonSize, NO , 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGRect br = CGRectMake(0, 0, buttonSize.width, buttonSize.height);
CGContextFillEllipseInRect(context, br);
CGContextStrokeEllipseInRect(context, br);
//theres a pretty circle, wanna draw other stuff? do it here..
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}