【问题标题】:Rounding color around borders of a button按钮边框周围的四舍五入颜色
【发布时间】:2011-09-11 13:52:41
【问题描述】:

我遇到了一个小问题,如下所示:

单元格有背景色,而按钮没有。尽管如此,它并没有给我圆润的边缘,而是角落。我该如何解决?

UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[[meerKnop layer] setCornerRadius:8.0f];
meerKnop.backgroundColor = [UIColor whiteColor];
meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
...        
[meerKnop addSubview:locationLabel];
...
[meerKnop addSubview:categoryLabel];

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[meerKnop addGestureRecognizer:swipe];
[swipe release];

[meerKnop addTarget:self action:@selector(alertPressed:) forControlEvents:UIControlEventTouchUpInside];
meerKnop.tag = incId;
[cell addSubview:meerKnop];

【问题讨论】:

    标签: iphone objective-c button colors tableview


    【解决方案1】:

    尝试设置按钮层的圆角半径。

    [button.layer setCornerRadius:10];
    

    如果你使用图层属性记得导入

    另外,使用:

    [[button layer] setMasksToBounds:YES];
    

    使用此代码,图层的角半径为 10.0,并且 -setMasksToBounds: 告诉按钮的图层屏蔽图层树中位于其下方的任何图层内容。这是必要的,以便图层遮住圆角。

    【讨论】:

    • 恐怕它对我不起作用。图层选项是否可能因为我的按钮定义了它的框架而不起作用?
    • @Manjunath 我已经编辑了答案(同行评审),这使它对我有用。我错过了[button.layer setMaskToBounds:YES]; 这条线。有了这个,它就可以了
    • @Joetjah:感谢您的提醒。
    【解决方案2】:

    将按钮的背景颜色更改为与单元格的背景颜色相同。它将处理圆角边缘,因为那里的颜色将与背景颜色相同。 sry我是新人..我现在只能想到这种方式..希望它有所帮助..

    【讨论】:

    • 这是一个不错的建议,但例如,当一切都是白色时,单元格看起来更丑陋。除此之外,像 iOS 这样的东西应该有这样的选择,尤其是在 Apple 如此面向 UI 的情况下......
    【解决方案3】:
    UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    

    将其更改为:

    UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
    

    已编辑:

    UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
    [[meerKnop layer] setCornerRadius:8.0f];
    meerKnop.backgroundColor = [UIColor redcolor];
    meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
    

    告诉我大的白色矩形出现在哪里? (我希望你已经清除了单元格背景颜色)。

    【讨论】:

    • 这个答案删除了我的边框并使按钮成为一个矩形。所以现在,它是一个白色的大矩形,里面有一些文字。我想你误解了这个问题
    猜你喜欢
    • 2016-03-25
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2023-01-12
    • 1970-01-01
    • 2012-08-04
    • 2023-03-26
    相关资源
    最近更新 更多