xcode 如何在 iPhone 中制作带有图像背景的圆形按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6338578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:15:49  来源:igfitidea点击:

how to make button round with image background in IPhone?

objective-cxcodeios4uibutton

提问by user4951

I have a round button, but i will make it have a backgroud with image.

我有一个圆形按钮,但我会让它有一个带有图像的背景。

but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?

但是当我这样做时,通过将 (what) 属性设置为图像,按钮变为矩形,因为图像是矩形。如何让按钮保持圆形?

回答by Craig White

Simply by doing this

只需这样做

#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;

回答by Vijay-Apple-Dev.blogspot.com

Tested Code:

测试代码:

.h

。H

    -(void)vijayWithApple; 

.m

.m

   -(void)vijayWithApple{

            NSLog(@"vijayWithApple Called");
   }


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"Micky.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(vijayWithApple) 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];

Restut

恢复

enter image description here

在此处输入图片说明

回答by ColonelRN

Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.

我为 UITableCell 找到的最佳解决方案。我将代码放在awakeFromNib 中并立即开始工作。