【问题标题】:How do I create a custom view class Programmatically? [closed]如何以编程方式创建自定义视图类? [关闭]
【发布时间】:2013-08-02 09:37:00
【问题描述】:

我想创建一个非常简单的 customView,上面有几个 UIlabel,我应该怎么做。任何教程或建议将不胜感激。我是新手,之前没试过。

我用 xib 试过这个。

@interface MyCustomView : UIView

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

@end

实施

#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

但我需要以编程方式进行,请帮助。谢谢

【问题讨论】:

  • 这个问题对于一个简单的 QA 问题来说太宽泛了。浏览上面的书,它以编程方式涵盖了所有内容。
  • @Ezimettekin 你想在已经存在的类中创建一个自定义视图或者你想用 nib 文件创建一个 UIView 类

标签: iphone ios ios6 custom-view


【解决方案1】:

这里有一个简单的方法,希望对你有帮助。

//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】:

    您说您不想使用 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;
    }
    

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

    如果您使用自动布局,您还想从 init 方法创建所有约束。

    如果你使用自动布局,你应该实现-layoutSubviews 方法。这将在适当的时候调用来布局您的子视图(例如,当您的视图框架发生变化时):

    - (void)layoutSubviews
    {
        self.myLabel.frame = ...;
    }
    

    通过layoutSubviews 方法,您可以访问self.bounds 以计算出当时视图的大小。这将让您知道正确对齐或包裹东西需要多少宽度/高度。

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

    【讨论】:

      【解决方案3】:

      更新您的类以实现initWithFrame: 以及initWithCoder:,然后您可以调用:

      [[MyCustomView alloc] initWithFrame:...];
      

      在您的代码中。因此,您已经以编程方式创建了一个实例(已配置)。

      - (id)initWithFrame:(CGRect)frame {
          if ((self = [super initWithFrame:frame])) {
              UILabel *label = [[UILabel alloc] initWithFrame:...];
              // configure the label
              [self addSubview:label];
          }
       return self;
      }
      

      【讨论】:

      • 如果我错了请纠正我: 1. 如果我想创建一个自定义视图,它的 width 是相对于它的 superView 那么没有操作(与superview) 可以在 customView 类中完成。只有在实际添加到 superView 时才能完成约束。 2.如果我不希望它的宽度是相对的,那么我可以简单地用一个固定的框架来初始化它,或者只是基于屏幕大小的东西 3.在 customView 我可以子视图(标签、文本字段等)以及它们对 customView 的约束
      • @Honey 它通常不是那么简单。视图现在通常将其自动调整大小掩码转换为约束,因此这取决于它的配置方式。您还可以使用布局子视图和更新约束功能来更新应用的规则...
      • 我不确定我是否理解你......我最终要问的是一个类是否可以相对于它的 superView 设置它的框架/约束——来自它自己的类?还是必须始终来自 superView?
      • 视图的框架是在它的超级视图边界的上下文中,但谁设置它并不重要。视图可以设置它自己的框架。尽管子视图了解父视图框架通常被归类为不好的做法......
      【解决方案4】:

      创建您自己的 UIView 子类,然后覆盖 initWithFrame 或 drawRect。重写 initWithFrame 对新手来说更容易。

      您的头文件 (.h) 应该以此开头:

      @interface YourOwnView : UIView
      
       ...
      
       @end
      

      然后,在实现文件 (.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;
       }
      

      您可能需要初始化程序中的额外数据或选项,例如标签的文本。在这种情况下,您可以定义一个带有额外参数的 init 方法,如下所示:

      - (id)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText;
      

      【讨论】:

        【解决方案5】:

        您可以将这些行放在您的 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];
        }
        

        这是一个非常简单的示例 .. 只需添加一个标签,您就可以通过编程方式添加任意数量的对象.. 使用您想要的位置和帧大小以及您想要的任何属性...

        也许您应该以编程方式搜索“UILabel..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          • 2016-07-02
          • 2012-08-22
          • 1970-01-01
          相关资源
          最近更新 更多