ios 为按钮添加边框和阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7923443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:05:18  来源:igfitidea点击:

Add border and shadow to the buttons

objective-ciosipad

提问by Prateek Chaubey

I want to add medium thick black border and shadow to the buttons that I have added in my iPad application. I have taken those buttons as custom as I have applied images on them. How can I add border and shadow to ht

我想为我在 iPad 应用程序中添加的按钮添加中等粗的黑色边框和阴影。我把这些按钮当作自定义按钮,因为我已经在它们上面应用了图像。如何为 ht 添加边框和阴影

回答by Arslan

To add shadows and border is simple.

添加阴影和边框很简单。

1) Add the QuartzCore framework to your target.
2) Import the framework header in the class where you want to add borders and shadows. (Or if you have custom class for the button then you can simple import this framework in that class.)
3) To add the border to the button use this code (where buttonis an IBOutletconnected with the button in interface):

1) 将 QuartzCore 框架添加到您的目标。
2) 在要添加边框和阴影的类中导入框架头文件。(或者,如果你有自定义类的按钮,然后就可以简单的导入这个框架在类)
3)边境添加到按钮使用此代码(其中button的一个IBOutlet与界面按钮连接):

[self.button.layer setBorderWidth:3.0];
[self.button.layer setBorderColor:[[UIColor blackColor] CGColor]];


4) To add the shadow to the button use the following code:


4) 要将阴影添加到按钮,请使用以下代码:

[self.button.layer setShadowOffset:CGSizeMake(5, 5)];
[self.button.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.button.layer setShadowOpacity:0.5];

You can play around with the values and see how it will affect the behaviour.

您可以使用这些值并查看它将如何影响行为。

回答by Wienke

If you were in Cocoa, you could use NSView's setShadow. (NSView is an ancestor of NSButton.) I don't see the equivalent method for UIView, so I don't think there is any simple way of doing that in iOS. Come to think of it, I don't think I've seen that effect in iOS apps, period.

如果你在 Cocoa,你可以使用 NSView 的 setShadow。(NSView 是 NSButton 的祖先。)我没有看到 UIView 的等效方法,所以我认为在 iOS 中没有任何简单的方法可以做到这一点。想想看,我不认为我在 iOS 应用程序中看到过这种效果。

But since you're using custom button images anyway, why not prepare images that include shadow and border?

但是既然您无论如何都在使用自定义按钮图像,为什么不准备包含阴影和边框的图像呢?