ios 如何以编程方式创建自定义视图类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17994473/
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
How do I create a custom view class Programmatically?
提问by Ezimet
I want to create a very simple customView with a few UIlabel on it, How should i do this . any tutorial or suggestion would be appreciated . I am new to this , didn't try before.
我想创建一个非常简单的 customView,上面有一些 UIlabel,我应该怎么做。任何教程或建议将不胜感激。我是新手,以前没有尝试过。
I tried this with xib.
我用xib试过这个。
@interface MyCustomView : UIView
@property (strong, nonatomic) IBOutlet UILabel *Label;
@end
Implementation
执行
#import "MyCustomTimer.h"
@implementation MyCustomView
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
@end
But i need to do it programmatically ,please help . thanks
但我需要以编程方式进行,请帮忙。谢谢
回答by Shankar BS
Here is a simple way, hope it helps you.
方法很简单,希望对你有帮助。
//in subclassed UIView
#import "CustomView.h"
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// initilize all your UIView components
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20,30, 200, 44)];
label1.text = @"i am label 1";
[self addSubview:label1]; //add label1 to your custom view
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20,80, 200, 44)];
label2.text = @"i am label 2";
[self addSubview:label2]; //add label2 to your custom view
[label1 release];//i am using without ARC, comment if u are using ARC
[label2 release];//i am using without ARC, comment if u are using ARC
}
return self;
}
// in your class where u want to use that view
#import "ViewController.h"
#import "CustomView.h"//import it
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//create your view where u want
CustomView *cv = [[CustomView alloc]initWithFrame:CGRectMake(10, 10, 230, 400)]; //create an instance of your custom view
[self.view addSubview:cv]; // add to your main view
[cv release];//comment if u are using ARC
}
回答by Mike Weller
You say you don't want to use a XIB and want to do it all programmatically.
您说您不想使用 XIB 并希望以编程方式完成所有操作。
You need to implement the initWithFrame:
method:
您需要实现该initWithFrame:
方法:
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// create/initialize your subviews here
self.myLabel = [[UILabel alloc] init...];
// configure the label
self.myLabel.font = ...;
self.myLabel.autoresizingMask = ...;
[self addSubview:self.myLabel];
}
return self;
}
So you create and configure your controls (fonts, colours, autoresizing masks etc.) and add them as subviews, all from the initWithFrame:
method. You probably want to break the code out into different methods to keep things clean.
所以你创建和配置你的控件(字体、颜色、自动调整大小蒙版等)并将它们添加为子视图,所有这些都来自initWithFrame:
方法。您可能希望将代码分解为不同的方法以保持整洁。
If you are using autolayout, you also want to create all your constraints from the init method.
如果您使用自动布局,您还希望从 init 方法创建所有约束。
If you are notusing autolayout, you should implement the -layoutSubviews
method. This will be called at appropriate times to layout your subviews (e.g. when the frame of your view changes):
如果您不使用自动布局,则应实现该-layoutSubviews
方法。这将在适当的时间调用以布局您的子视图(例如,当您的视图框架发生变化时):
- (void)layoutSubviews
{
self.myLabel.frame = ...;
}
From the layoutSubviews
method you can access self.bounds
to figure out the size of the view at that time. This will let you know how much width/height you have to align or wrap things correctly.
从该layoutSubviews
方法中可以访问self.bounds
到计算出当时视图的大小。这将让您知道要正确对齐或包裹物品的宽度/高度。
When it comes to creating an instance of your view, just use [[MyCustomView alloc] init]
(which will call initWithFrame:
with an empty rect) or [[MyCustomView alloc] initWithFrame:...]
. Set its frame and add it to some view. The layoutSubviews
method will be called at all the appropriate times and it will be layed out accordingly.
在创建视图实例时,只需使用[[MyCustomView alloc] init]
(它将initWithFrame:
使用空矩形调用)或[[MyCustomView alloc] initWithFrame:...]
. 设置它的框架并将其添加到某个视图中。该layoutSubviews
方法将在所有适当的时间被调用,并将相应地进行布局。
回答by Wain
Update your class to implement initWithFrame:
as well as initWithCoder:
, then you can call:
更新您的类以实现initWithFrame:
以及initWithCoder:
,然后您可以调用:
[[MyCustomView alloc] initWithFrame:...];
In your code. Thus you have created an instance (that is configured) programatically.
在你的代码中。因此,您以编程方式创建了一个实例(已配置)。
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
UILabel *label = [[UILabel alloc] initWithFrame:...];
// configure the label
[self addSubview:label];
}
return self;
}
回答by v-i-s-h-a-l
You can place these lines in your initWithFrame:
您可以将这些行放在 initWithFrame 中:
self = [super initWithFrame:frame]
if (self!=null) {
// if self has been allocated
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 40)];
[label setText:@"Label"];
[label setTextAlignment:NSTextAlignmentJustified];
[self addSubview:label];
}
Its a very simple example .. to add just one label you can add as many objects you want programatically.. with your desired locations and frame sizes and whatever properties you want...
这是一个非常简单的例子.. 只需添加一个标签,您就可以以编程方式添加任意数量的对象.. 具有您想要的位置和框架大小以及您想要的任何属性......
perhaps you should have searched "UILabel programatically..
也许您应该以编程方式搜索“UILabel ..
回答by Zeke
Create your own class subclassing UIView, then override initWithFrame or drawRect. Overriding initWithFrame is easier for a newbie.
创建您自己的 UIView 子类,然后覆盖 initWithFrame 或 drawRect。对于新手来说,覆盖 initWithFrame 更容易。
Your header (.h) file should start with this:
您的标头 (.h) 文件应以以下内容开头:
@interface YourOwnView : UIView
...
@end
Then, in the implementation file (.m) you should implement your own initWithFrame.
然后,在实现文件 (.m) 中,您应该实现自己的 initWithFrame。
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//ADDING NEW CONTENT
UILabel *yourLabel = [[UILabel alloc]init];
//CUSTOMIZE your label's font, text, etc.
....
//Add your label to your own view
[self addSubview:yourLabel];
}
return self;
}
You might need extra data or options in the initializer, for example the text for labels. In this case you could define an init method with extra arguments like this:
您可能需要初始化程序中的额外数据或选项,例如标签文本。在这种情况下,您可以定义一个带有额外参数的 init 方法,如下所示:
- (id)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText;