如何绕一个按钮的角落
问题描述:
我有一个矩形图像(jpg),并希望用它来填充xcode中具有圆角的按钮的背景。如何绕一个按钮的角落
我写了下面:
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
CGRect frame = CGRectMake(x, y, cardWidth, cardHeight);
button.frame = frame;
[button setBackgroundImage:backImage forState:UIControlStateNormal];
然而,按钮,我用这种方法得到不具有其边角圆润:这是不是一个普通的矩形长相酷似我的原始图像。我怎样才能得到一个圆角的图像来代表我的按钮?
谢谢!
答
我想我得到了你的问题,我试着用UITextArea以下解决方案,我想这也适用于UIButton。
首先进口这在你的.m文件 -
#import <QuartzCore/QuartzCore.h>
,然后在loadView
方法中添加以下行
yourButton.layer.cornerRadius = 10; // this value vary as per your desire
yourButton.clipsToBounds = YES;
希望这对你的作品,如果是做沟通。
答
如果你想有一个圆角只有一个角落或两个角,等...阅读岗位:
[ObjC] - 的UIButton与圆角 - http://goo.gl/kfzvKP
这是一个XIB /故事板的子类。无需编写代码即可导入和设置边框。
答
进口QuartCore框架,如果它不存在于现有的项目,然后导入#import <QuartzCore/QuartzCore.h>
在viewcontroller.m
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect]];
CGRect frame = CGRectMake(x, y, width, height); // set values as per your requirement
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;
答
可以实现通过这种运行属性
我们可以自定义button.just看附加的截图。
亲切注意:
在运行时的属性来改变边框的颜色按照此说明
创建.h文件类别类的CALayer
-
的
@property(nonatomic, assign) UIColor* borderIBColor;
- 在M档
:
-(void)setBorderIBColor:(UIColor*)color { self.borderColor = color.CGColor; } -(UIColor*)borderIBColor { return [UIColor colorWithCGColor:self.borderColor]; }
现在开始设置边框颜色检查截图
感谢
答
UIButton* closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 90, 35)];
//Customise this button as you wish then
closeBtn.layer.cornerRadius = 10;
closeBtn.layer.masksToBounds = YES;//Important
答
第一设定宽度= 100和高度按钮
YourBtn1.layer.cornerRadius=YourBtn1.Frame.size.width/2;
YourBtn1.layer.borderColor=[uicolor blackColor].CGColor;
YourBtn1.layer.borderWidth=1.0f;
答
的= 100的SWIFT:
button.layer.cornerRadius = 10.0
答
更新的夫特3:
下面使用代码使UIButton转角:
yourButtonOutletName.layer.cornerRadius = 0.3 *
yourButtonOutletName.frame.size.height
非常感谢。这不仅是我需要的,但是你回答了我的下一个问题,那就是确定角落半径是否可以调整。 – double07 2011-02-21 22:57:38
非常感谢 - 尤其是对于clipsToBounds提示。 – Chris 2012-08-01 01:56:02