【问题标题】:how to make button round with image background in IPhone?如何在 iPhone 中使用图像背景制作圆形按钮?
【发布时间】:2011-09-14 09:05:56
【问题描述】:

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

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

【问题讨论】:

  • 为什么不使用圆形图像?

标签: objective-c xcode ios4 uibutton


【解决方案1】:

我发现的最佳解决方案尤其适用于 UITableCell。我将代码放入 awakeFromNib 并立即工作。

【讨论】:

  • 一个例子会很有帮助。资源链接也不错。
【解决方案2】:

测试代码:

.h

    -(void)vijayWithApple; 

.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];

恢复

【讨论】:

    【解决方案3】:

    只需这样做

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

    【讨论】:

    • 同时添加 myButton.clipsToBounds = YES;
    • @JomanJi 感谢 clipToBound 提示。那是缺失的环节。使用情节提要时,标记“剪辑子视图”(并将 layer.cornerRadius 添加到运行时属性)。
    • @asgoth 我不知道故事板技巧!好吧,你每天都在学习……谢谢!
    猜你喜欢
    • 2021-08-05
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多