gpt4 book ai didi

ios - 如何在 UIButton 上只添加顶部边框?

转载 作者:IT王子 更新时间:2023-10-29 07:43:00 24 4
gpt4 key购买 nike

我知道如何在 iOS 7 中为按钮添加边框,代码如下:

[[myButton layer] setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[[myButton layer] setBorderWidth:1];
[[myButton layer] setCornerRadius:15];

但是我怎样才能只添加一个边框呢? 我只想添加顶部边框

最佳答案

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 1)];
lineView.backgroundColor = [UIColor redColor];
[btn addSubview:lineView];

您可以对每个边框执行相同的操作。添加多个 UIView,您可以添加底部和左侧或顶部和右侧或您想要的任何边框。

即底部和左侧:

UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, btn.frame.size.height - 1.0f, btn.frame.size.width, 1)];
bottomBorder.backgroundColor = [UIColor redColor];

UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(1, 0, 1, btn.frame.size.height)];
leftBorder.backgroundColor = [UIColor redColor];

[btn addSubview:bottomBorder];
[btn addSubview:leftBorder];

如果不使用ARC,记得在添加 subview 后释放UIViews(或者使用autorelease)。

关于ios - 如何在 UIButton 上只添加顶部边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com