问题
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.
I tried this with 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
回答1:
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
}
回答2:
You say you don't want to use a XIB and want to do it all programmatically.
You need to implement the initWithFrame:
method:
- (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.
If you are using autolayout, you also want to create all your constraints from the init method.
If you are not using 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):
- (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.
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.
回答3:
Update your class to implement initWithFrame:
as well as initWithCoder:
, then you can call:
[[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;
}
回答4:
Create your own class subclassing UIView, then override initWithFrame or drawRect. Overriding initWithFrame is easier for a newbie.
Your header (.h) file should start with this:
@interface YourOwnView : UIView
...
@end
Then, in the implementation file (.m) you should implement your own 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:
- (id)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText;
回答5:
You can place these lines in your 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..
来源:https://stackoverflow.com/questions/17994473/how-do-i-create-a-custom-view-class-programmatically