Xcode 圆形视图和按钮?

Xcode 圆形视图和按钮?,xcode,uitableview,uiview,uibutton,Xcode,Uitableview,Uiview,Uibutton,我正在尝试复制一个按钮,如下面的按钮(YO ap) 这个按钮漂浮在桌子视图的正上方,但是视图是完美的圆形,你可以通过下面的k字母如何完美地突出来看到这一点。是否可以创建圆形UIView?你知道阴影效果是如何产生的吗 感谢要创建圆形视图,您可以更改视图图层的角半径属性。 例如,您可以在某处创建一个正方形(宽度==高度)按钮,在viewController中为其创建一个插座(例如,@property(弱、非原子)IBOutlet ui按钮*bMyButton;),然后将其角半径设置为: - (vo

我正在尝试复制一个按钮,如下面的按钮(YO ap)

这个按钮漂浮在桌子视图的正上方,但是视图是完美的圆形,你可以通过下面的k字母如何完美地突出来看到这一点。是否可以创建圆形UIView?你知道阴影效果是如何产生的吗


感谢

要创建圆形视图,您可以更改视图图层的
角半径
属性。 例如,您可以在某处创建一个正方形(宽度==高度)按钮,在viewController中为其创建一个插座(例如,
@property(弱、非原子)IBOutlet ui按钮*bMyButton;
),然后将其角半径设置为:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bMyButton.layer.cornerRadius = self.bMyButton.frame.size.height / 2;
}
我认为,要使按钮“浮动”在tableView之上,您应该将其添加为俯视图的子视图(与tableView处于同一层次)。 最后,您可以通过约束(自动布局)将视图“附加”到右下角。您可以在此处阅读有关自动布局的更多信息:

至于阴影,我从来没有这样做过,但我想,你应该看看其他的
图层
属性:
阴影半径
阴影偏移量
阴影不透明度
,等等

例如:

self.bMyButton.layer.shadowRadius = self.bMyButton.frame.size.width / 2 + 5;
请注意,UIView可能不会被剪裁到其边界。例如,UIImageView就是这种情况。如果需要圆形UIImageView,还必须将
masksToBounds
设置为
YES

self.imgMyImageView.layer.masksToBounds = YES;

要生成圆形视图,可以更改视图图层的
cornerRadius
特性。 例如,您可以在某处创建一个正方形(宽度==高度)按钮,在viewController中为其创建一个插座(例如,
@property(弱、非原子)IBOutlet ui按钮*bMyButton;
),然后将其角半径设置为:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bMyButton.layer.cornerRadius = self.bMyButton.frame.size.height / 2;
}
我认为,要使按钮“浮动”在tableView之上,您应该将其添加为俯视图的子视图(与tableView处于同一层次)。 最后,您可以通过约束(自动布局)将视图“附加”到右下角。您可以在此处阅读有关自动布局的更多信息:

至于阴影,我从来没有这样做过,但我想,你应该看看其他的
图层
属性:
阴影半径
阴影偏移量
阴影不透明度
,等等

例如:

self.bMyButton.layer.shadowRadius = self.bMyButton.frame.size.width / 2 + 5;
请注意,UIView可能不会被剪裁到其边界。例如,UIImageView就是这种情况。如果需要圆形UIImageView,还必须将
masksToBounds
设置为
YES

self.imgMyImageView.layer.masksToBounds = YES;

要生成圆形视图,可以更改视图图层的
cornerRadius
特性。 例如,您可以在某处创建一个正方形(宽度==高度)按钮,在viewController中为其创建一个插座(例如,
@property(弱、非原子)IBOutlet ui按钮*bMyButton;
),然后将其角半径设置为:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bMyButton.layer.cornerRadius = self.bMyButton.frame.size.height / 2;
}
我认为,要使按钮“浮动”在tableView之上,您应该将其添加为俯视图的子视图(与tableView处于同一层次)。 最后,您可以通过约束(自动布局)将视图“附加”到右下角。您可以在此处阅读有关自动布局的更多信息:

至于阴影,我从来没有这样做过,但我想,你应该看看其他的
图层
属性:
阴影半径
阴影偏移量
阴影不透明度
,等等

例如:

self.bMyButton.layer.shadowRadius = self.bMyButton.frame.size.width / 2 + 5;
请注意,UIView可能不会被剪裁到其边界。例如,UIImageView就是这种情况。如果需要圆形UIImageView,还必须将
masksToBounds
设置为
YES

self.imgMyImageView.layer.masksToBounds = YES;

要生成圆形视图,可以更改视图图层的
cornerRadius
特性。 例如,您可以在某处创建一个正方形(宽度==高度)按钮,在viewController中为其创建一个插座(例如,
@property(弱、非原子)IBOutlet ui按钮*bMyButton;
),然后将其角半径设置为:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bMyButton.layer.cornerRadius = self.bMyButton.frame.size.height / 2;
}
我认为,要使按钮“浮动”在tableView之上,您应该将其添加为俯视图的子视图(与tableView处于同一层次)。 最后,您可以通过约束(自动布局)将视图“附加”到右下角。您可以在此处阅读有关自动布局的更多信息:

至于阴影,我从来没有这样做过,但我想,你应该看看其他的
图层
属性:
阴影半径
阴影偏移量
阴影不透明度
,等等

例如:

self.bMyButton.layer.shadowRadius = self.bMyButton.frame.size.width / 2 + 5;
请注意,UIView可能不会被剪裁到其边界。例如,UIImageView就是这种情况。如果需要圆形UIImageView,还必须将
masksToBounds
设置为
YES

self.imgMyImageView.layer.masksToBounds = YES;

如果要以编程方式添加
ui按钮

//margin
CGFloat margin = 10.f;
//your image
UIImage *image = [UIImage imageNamed:@"side_menu.png"];
//create a button of custom type
UIButton *circularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//set background colour (if your image includes background colour then you may don't need this)
circularBtn.backgroundColor = [UIColor colorWithRed:245.f/255.f green:71.f/255.f blue:64.f/255.f alpha:1.f];
//set your image
[circularBtn setImage:image forState:UIControlStateNormal];
//dynamic frame
circularBtn.frame = CGRectMake(self.view.frame.size.width - (image.size.width * 2.f + margin ), 
self.view.frame.size.height - (image.size.height * 2.f + margin), 
(image.size.width * 2.f), (image.size.height * 2.f));
//add to self or any other view you want
[self.view addSubview:circularBtn];
//round the button
circularBtn.layer.cornerRadius = circularBtn.frame.size.width/2.f;
//add the shadow
circularBtn.layer.shadowColor = [UIColor blackColor].CGColor;
circularBtn.layer.shadowOpacity = 0.7f;
应该是这样的,


在我的示例中,如果您希望以编程方式添加
ui按钮,我将使用大小为40x40(WxH)和@2x的透明图像

//margin
CGFloat margin = 10.f;
//your image
UIImage *image = [UIImage imageNamed:@"side_menu.png"];
//create a button of custom type
UIButton *circularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//set background colour (if your image includes background colour then you may don't need this)
circularBtn.backgroundColor = [UIColor colorWithRed:245.f/255.f green:71.f/255.f blue:64.f/255.f alpha:1.f];
//set your image
[circularBtn setImage:image forState:UIControlStateNormal];
//dynamic frame
circularBtn.frame = CGRectMake(self.view.frame.size.width - (image.size.width * 2.f + margin ), 
self.view.frame.size.height - (image.size.height * 2.f + margin), 
(image.size.width * 2.f), (image.size.height * 2.f));
//add to self or any other view you want
[self.view addSubview:circularBtn];
//round the button
circularBtn.layer.cornerRadius = circularBtn.frame.size.width/2.f;
//add the shadow
circularBtn.layer.shadowColor = [UIColor blackColor].CGColor;
circularBtn.layer.shadowOpacity = 0.7f;
应该是这样的,


在我的示例中,如果您希望以编程方式添加
ui按钮,我将使用大小为40x40(WxH)和@2x的透明图像

//margin
CGFloat margin = 10.f;
//your image
UIImage *image = [UIImage imageNamed:@"side_menu.png"];
//create a button of custom type
UIButton *circularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//set background colour (if your image includes background colour then you may don't need this)
circularBtn.backgroundColor = [UIColor colorWithRed:245.f/255.f green:71.f/255.f blue:64.f/255.f alpha:1.f];
//set your image
[circularBtn setImage:image forState:UIControlStateNormal];
//dynamic frame
circularBtn.frame = CGRectMake(self.view.frame.size.width - (image.size.width * 2.f + margin ), 
self.view.frame.size.height - (image.size.height * 2.f + margin), 
(image.size.width * 2.f), (image.size.height * 2.f));
//add to self or any other view you want
[self.view addSubview:circularBtn];
//round the button
circularBtn.layer.cornerRadius = circularBtn.frame.size.width/2.f;
//add the shadow
circularBtn.layer.shadowColor = [UIColor blackColor].CGColor;
circularBtn.layer.shadowOpacity = 0.7f;
应该是这样的,


在我的示例中,如果您希望以编程方式添加
ui按钮,我将使用大小为40x40(WxH)和@2x的透明图像

//margin
CGFloat margin = 10.f;
//your image
UIImage *image = [UIImage imageNamed:@"side_menu.png"];
//create a button of custom type
UIButton *circularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//set background colour (if your image includes background colour then you may don't need this)
circularBtn.backgroundColor = [UIColor colorWithRed:245.f/255.f green:71.f/255.f blue:64.f/255.f alpha:1.f];
//set your image
[circularBtn setImage:image forState:UIControlStateNormal];
//dynamic frame
circularBtn.frame = CGRectMake(self.view.frame.size.width - (image.size.width * 2.f + margin ), 
self.view.frame.size.height - (image.size.height * 2.f + margin), 
(image.size.width * 2.f), (image.size.height * 2.f));
//add to self or any other view you want
[self.view addSubview:circularBtn];
//round the button
circularBtn.layer.cornerRadius = circularBtn.frame.size.width/2.f;
//add the shadow
circularBtn.layer.shadowColor = [UIColor blackColor].CGColor;
circularBtn.layer.shadowOpacity = 0.7f;
应该是这样的,

在我的示例中,我希望看到大小为40x40(WxH)和@2x的透明图像