【问题标题】:UIButton with rounded corners using square setBackgroundImage使用方形 setBackgroundImage 的带有圆角的 UIButton
【发布时间】:2012-05-11 15:47:44
【问题描述】:

所以我创建了一个按钮。我希望它有圆角而不制作圆角 .png 文件 我可以设置背景图像。我可以绕过角落。但是,当我设置背景图像时,圆角消失了。

很久以前我做过一次,但似乎无法让它再次工作。如何做到这一点?

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(goSpecials)
  forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Specials" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:(15.0)];
[button1 setTitleColor:[self colorWithHexString:buttonColor] forState:UIControlStateNormal];
button1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"button.png"]]; 
 //[button1 setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
button1.frame = CGRectMake(20.0, 232.0, 135.0, 32.0);
button1.layer.borderColor = [UIColor blackColor].CGColor;
button1.layer.borderWidth = 0.5f;
button1.layer.cornerRadius = 8.0f;
[self.view addSubview:button1];

按钮.png:

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    将 maskToBounds 属性添加到您的按钮。

    button1.layer.masksToBounds = YES;
    

    【讨论】:

    • 太棒了。解决了我的问题。 +1
    猜你喜欢
    • 2016-09-28
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2023-04-01
    相关资源
    最近更新 更多