【问题标题】:How to create a round button?如何创建一个圆形按钮?
【发布时间】:2011-08-05 07:15:46
【问题描述】:

我想创建一个圆形圆形按钮。这个按钮应该看起来像一个圆圈。

这段代码给出了圆形矩形按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame     = CGRectMake(100, 100, 100, 30);
UIImage  *image  = [UIImage imageNamed:@"01.png"];
[button setImage:image forState:UIControlStateNormal];
[image release];

我想出了如何创建一个圆角矩形按钮,但我想创建一个圆形按钮。我需要改变什么?

【问题讨论】:

标签: iphone objective-c xcode


【解决方案1】:

测试代码:

.h

 #import <QuartzCore/QuartzCore.h>

-(void)roundButtonDidTap:(UIButton*)tappedButton;

.m

#define ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere

-(void)roundButtonDidTap:(UIButton*)tappedButton{

    NSLog(@"roundButtonDidTap Method Called");

}



UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"TimoonPumba.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

//width and height should be same value
button.frame = CGRectMake(0, 0, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT);

//Clip/Clear the other pieces whichever outside the rounded corner
button.clipsToBounds = YES;

//half of the width
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;

[self.view addSubview:button];

结果

这个概念中的几何

【讨论】:

  • 只是一个更新:你还需要#import
  • @raaz 谢谢哥们,如果你以后发现类似的东西,你可以编辑代码:)
  • +1 用于添加//half of the width 评论。通过将按钮的高度/宽度除以 2,您可以使 cornerRadius 舍入。
  • 无论如何要使圆圈内的区域可点击?我发现原始矩形内的任何内容都是可点击的。
  • 我建议您在此按钮的超级视图上为可点击位置再添加一个子视图。
【解决方案2】:

故事板选项(应该适用于 Swift 或 ObjC)-

如果您更喜欢在 Storyboard 中工作,还有另一种选择。

首先,将宽度和高度设置为相同的值以制作完美的正方形。

其次,输入以下属性。重要 - 将 layer.cornerRadius 的值设为宽度的一半。

然后,当您运行应用程序时,您的按钮将是圆形的。

【讨论】:

  • 显然是一个!根据您的方式或通过代码,按钮宽度必须设置为等于其高度。
【解决方案3】:

试试这个,它对我有用。仅当您将按钮或任何视图设为方形时,它才会使您的按钮或任何视图变为圆形,即宽度应等于高度。

yourButton.layer.cornerRadius = yourButton.bounds.size.width/2;

【讨论】:

  • 是的,谢谢伙计。
【解决方案4】:

在 IOS 8 上的 Xcode 7.0.0 中,此代码非常适合我。 前提是按钮框架的长度和高度相同。

myButton.clipsToBounds = YES;
myButton.layer.cornerRadius = myButton.layer.frame.size.width/2;

【讨论】:

    【解决方案5】:
    UIButton *myButton=[UIButton buttonWithType:UIButtonTypeCustom];
    myButton.frame = CGRectMake(50,50,30,30);
    [myButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [myButton setImage:[UIImage imageNamed:@"sarabjit.png"] forState:UIControlStateNormal];
    [self.view addSubView:myButton];
    

    【讨论】:

      【解决方案6】:

      使用 Swift 就像答案一样 Vijay-Apple-Dev.blogspot

      let ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere   
      
      override func viewDidLoad() {
         super.viewDidLoad()
      
         var button = UIButton.buttonWithType(.Custom) as UIButton
         button.frame = CGRectMake(160, 100, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT)
      
         button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT / 2.0
         button.layer.borderColor = UIColor.redColor().CGColor
         button.layer.borderWidth = 2.0
      
         button.setImage(UIImage(named:"TimoonPumba.png"), forState: .Normal)
         button.addTarget(self, action: "roundButtonDidTap", forControlEvents: .TouchUpInside)
         button.clipsToBounds = true
      
         view.addSubview(button)
      }
      
      func roundButtonDidTap() {
         NSLog(@"roundButtonDidTap Method Called");
      }
      

      【讨论】:

        【解决方案7】:
        override func viewDidLoad() {
            super.viewDidLoad()
        
            let facebookButton = UIButton()
            facebookButton.frame = CGRectMake(30, 200, 150, 150)
            facebookButton.layer.cornerRadius = 75
            facebookButton.layer.masksToBounds = true
            self.view.addSubview(facebookButton)
            }
        

        ** 要制作圆形按钮,您必须为按钮提供相同的高度和相同的宽度,并且应该将一半的值赋予角半径 **

        【讨论】:

          【解决方案8】:

          第 1 步。使用[UIButton buttonWithType:UIButtonTypeCustom] 创建按钮。

          第 2 步。CGRectMake(100, 100, 100, 30) 是一个矩形。也许您想改用图像的大小,如下所示:

          CGRect frame = CGRectMake(100, 100, 0, 0);
          frame.size = img.size;
          button.frame = frame;
          

          【讨论】:

            【解决方案9】:

            带有圆形图像的圆形矩形按钮不能满足圆形按钮的需要。即使它们是自定义按钮。这是因为,如果这个按钮在按钮大小范围内并且没有被剪裁,那么即使在圆形部分(这只是一个 .png 图像)之外,这个按钮也会响应点击。

            这里给出的第一个答案(并且只有#vijay-apple-dev 的第一个)是正确的。您需要剪辑按钮框架的边界,以阻止它在图像外响应

            【讨论】:

              【解决方案10】:

              制作一个

              [UIButton buttonWithType:UIButtonTypeCustom];
              

              并尝试使用圆角半径属性

                 button.layer.cornerRadius = button.bounds.size.width/2 / 2.0
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-07-23
                • 2019-12-24
                • 1970-01-01
                • 2023-04-11
                • 1970-01-01
                • 2017-05-23
                • 2015-07-27
                • 2017-03-04
                相关资源
                最近更新 更多