如何以编程方式创建自定义视图类?

Ezi*_*met 13 iphone custom-view ios ios6

我想创建一个非常简单的customView,上面有一些UIlabel,我该怎么办呢.任何教程或建议将不胜感激.我是新来的,之前没试过.

我用xib试过了.

@interface MyCustomView : UIView

@property (strong, nonatomic) IBOutlet UILabel *Label;

@end
Run Code Online (Sandbox Code Playgroud)

履行

#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
Run Code Online (Sandbox Code Playgroud)

但我需要以编程方式进行,请帮忙.谢谢

Sha*_* BS 29

这是一个简单的方法,希望它能帮到你.

//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
 }
Run Code Online (Sandbox Code Playgroud)



Mik*_*ler 5

您说您不想使用XIB并希望以编程方式完成所有操作.

您需要实现该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;
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以创建和配置控件(字体,颜色,自动调整蒙版等)并将它们添加为子视图,所有这些都来自initWithFrame:方法.您可能希望将代码分解为不同的方法以保持清洁.

如果使用autolayout,还需要从init方法创建所有约束.

如果您使用autolayout,则应实现该-layoutSubviews方法.这将在适当的时候调用以布置子视图(例如,当视图的框架发生变化时):

- (void)layoutSubviews
{
    self.myLabel.frame = ...;
}
Run Code Online (Sandbox Code Playgroud)

从该layoutSubviews方法中,您可以访问self.bounds以确定当时视图的大小.这将让您知道您需要对齐或正确包装的宽度/高度.

在创建视图实例时,只需使用[[MyCustomView alloc] init](将initWithFrame:使用空矩形调用)或[[MyCustomView alloc] initWithFrame:...].设置其框架并将其添加到某个视图.该layoutSubviews方法将在所有适当的时间调用,并相应地进行布局.