简体   繁体   English

圆形 UIButton

[英]Round UIButton

I want to know whether drawing a round UIButton(not rounded rect) is possible.我想知道是否可以绘制圆形 UIButton(不是圆形矩形)。

When I add a round image in a UIButton of type custom, then it looks like a round button.当我在自定义类型的 UIButton 中添加圆形图像时,它看起来像一个圆形按钮。 But at the moment the button is clicked the boundary of the button becomes visible, so that it looks like a square button, then again when the click ends it looks like a round button.但是在单击按钮的那一刻,按钮的边界变得可见,因此它看起来像一个方形按钮,然后在单击结束时再次看起来像一个圆形按钮。
I want the button to look like a round button even at the moment the click happens.即使在点击发生的那一刻,我也希望按钮看起来像一个圆形按钮。 is this possible?这可能吗?

Tested Code:测试代码:

.h 。H

    -(void)vijayWithApple; 

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

Result结果

在此处输入图像描述

Just create two rounded button images, one for the pressed state and one for unpressed.只需创建两个圆形按钮图像,一个用于按下 state,一个用于未按下。

In IB, create a UIButton of type Custom.在 IB 中,创建自定义类型的 UIButton。 Go to the section where you set a background image, and set the dropdown with "All" to normal - now set the image to your unpressed image. Go 到您设置背景图像的部分,并将“全部”的下拉菜单设置为正常 - 现在将图像设置为未按下的图像。

Then change the dropdown to "Selected", and put in your pressed image.然后将下拉菜单更改为“已选择”,然后放入您按下的图像。

Now change the fill type to be aspect fit, and make the button sqare.现在将填充类型更改为宽高比,并使按钮为方形。 Use as a normal UIButton anywhere.在任何地方用作普通 UIButton。 You can of course also easily do this all programaitcally with similar steps (setting images for UIControlStateNormal and UIControlStateSelected).当然,您也可以通过类似的步骤(为 UIControlStateNormal 和 UIControlStateSelected 设置图像)以编程方式轻松完成所有这些操作。

You'd probably have to create a custom control for that, but do you really need a round button?您可能必须为此创建一个自定义控件,但您真的需要一个圆形按钮吗?

Every platform has its UI conventions, and the iPhone is no exception.每个平台都有其 UI 约定,iPhone 也不例外。 Users expect the buttons to be rounded rectangles.用户希望按钮是圆角矩形。

UPDATE in response to comment:更新以回应评论:

If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.如果我做对了,那么您不是在寻找圆形按钮,而是在寻找可点击(可触摸)的图像。

You can use an UIImageView and its touchesBegan method.您可以使用UIImageView及其touchesBegan方法。

UPDATE 2:更新 2:

Wait a second.等一等。 What kind of radio button are we talking about?我们在谈论什么样的单选按钮? For some reason I thought you were trying to imitate a real radio.出于某种原因,我以为你是在模仿真正的收音机。 If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker .如果您在谈论单选按钮组,请使用UISegmentedControlUIPicker

UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];

but.layer.cornerRadius=50;

take the help of this site for creating Round Button on iPhone.借助本网站在 iPhone 上创建圆形按钮。 You can get many custom controls:)您可以获得许多自定义控件:)

http://www.cocoacontrols.com/controls http://www.cocoacontrols.com/controls

http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton

H@PPY CODING ! H@PPY 编码!

Thanks & regards, Gautam Tiwari谢谢和问候, Gautam Tiwari

From what I know of the SDK (admittedly I have very little experience beyond tutorials in books/screencasts), you'd have to create your own control to have a radio button.根据我对 SDK 的了解(诚然,除了书籍/截屏教程之外,我几乎没有经验),您必须创建自己的控件才能拥有单选按钮。 There are a couple of suggestions on how to implement that in this question .关于如何在这个问题中实现它有一些建议。

That said, from a user's perspective I feel like it would be better to stick to the native controls.也就是说,从用户的角度来看,我觉得坚持使用本机控件会更好。 iPhone users are used to specific controls (ie UITableViews or UIPickerViews) for this type of interaction. iPhone 用户习惯于使用特定控件(即 UITableViews 或 UIPickerViews)进行此类交互。 Straying from practically universal UI conventions tends to make the user experience more jarring.偏离实际通用的 UI 惯例往往会使用户体验更加不和谐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM