IOS Xcode 中的样式按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32336398/
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
Styling Buttons in IOS Xcode
提问by Muhammad Saifullah
I am new to IOS development. In windows phone development we use to define a style(appearance e.g) of the button control in app Global place and just use that style anywhere on any button in the app by assigning the style property.
我是IOS开发的新手。在 Windows Phone 开发中,我们使用在应用程序全局位置定义按钮控件的样式(例如外观),并通过分配样式属性在应用程序中的任何按钮上的任何位置使用该样式。
Is there anything similar in IOS XCode? I know I can customize the button in the property and appearance windows in XCode. But in this way I have to give style to each and every similar button in the app. and if there is any change I have to change it everywhere. What IOS developers do in these kind situations?
IOS XCode 中是否有类似的东西?我知道我可以在 XCode 的属性和外观窗口中自定义按钮。但通过这种方式,我必须为应用程序中的每个类似按钮赋予样式。如果有任何改变,我必须到处改变它。在这种情况下,IOS 开发人员会做什么?
采纳答案by NRitH
If you want to style allbuttons in your app in a standard way, then you would style the appearance proxy for UIButton
. You get one by calling the class method UIButton.appearance()
, and then styling the returned value the way you want your buttons to look. You can also do a more fine-grained appearance by calling methods like UIButton.appearanceWhenContainedIn()
.
如果您想以标准方式为应用程序中的所有按钮设置样式,那么您可以为UIButton
. 您可以通过调用类方法获得一个UIButton.appearance()
,然后按照您希望按钮的外观样式设置返回值。您还可以通过调用诸如UIButton.appearanceWhenContainedIn()
.
回答by MarkHim
Create a UIButton subclass. For example for creating a UIButton with rounded corners and a border:
创建一个 UIButton 子类。例如,创建一个带有圆角和边框的 UIButton:
Header file
头文件
#import <Foundation/Foundation.h>
@interface XYRoundedCornerButton : UIButton
@end
Implementation file
实现文件
...import headers etc...
@implementation XYRoundedCornerButton
- (void)awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderRadius = 1.0f;
self.layer.borderColor = UIColor.redColor;
}
@end
回答by simardeep singh
If you are using storyboard, You don't need to add any code in class. Just drag one button and set all the properties you want, whether background colour, corner radius etc, whatever you want. And then either copy it and paste wherever you want or select the button, ALT+DRAG it .
如果您使用的是故事板,则无需在课堂上添加任何代码。只需拖动一个按钮并设置您想要的所有属性,无论是背景颜色、圆角半径等,无论您想要什么。然后将其复制并粘贴到您想要的任何位置或选择按钮 ALT+DRAG it 。