自定义UIView

Whenever we want to add an instance of this custom view to the visible screen, we now need to:

  1. load the Nib, and
  2. extract the actual view from the array of loaded objects (because we did not connect it to any outlet on the File’s Owner), and finally
  3. add the newly-instantiated custom view as a subview and set its frame.

Something like this:

MyCustomView customView = nil; NSArray* elements = [[NSBundle mainBundle] loadNibNamed: @”MyCustomView” // (1) owner: nil options: nil]; for (id anObject in elements) { // (2) if ([anObject isKindOfClass:[MyCustomView class]]) { customView = anObject; break; } } [self.view addSubview:customView]; // (3) customView.frame = CGRectMake(100.0, 100.0, 400.0, 90.0);

This is tedious because most of it is generic code that is repeated every time we instantiate a custom view from a Nib. Therefore, my colleague Michael moved all boilerplate code to a UIView category, resulting in the following class method:

// UIView+NibLoading.m

  • (UIView) loadInstanceFromNib { UIView result = nil; NSArray* elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) owner: nil options: nil]; for (id anObject in elements) { if ([anObject isKindOfClass:[self class]]) { result = anObject; break; } } return result; }

With this category, the instantiation code becomes much sleeker:

MyCustomView* customView = [MyCustomView loadInstanceFromNib]; [self.view addSubview:customView]; customView.frame = CGRectMake(100.0, 100.0, 400.0, 90.0);

自定义UIView

时间: 2025-01-02 19:16:56

自定义UIView的相关文章

xib自定义UIView报错误 "forUndefinedKey:]: this class is not key value coding-compliant for the key"

使用xib自定义UIView的时候, 需要将控件拖拽成属性, 在viewController加载自定义view的时候报错误 "forUndefinedKey:]: this class is not key value coding-compliant for the key" 查找很久,终于发现是xib 的FILE'OWNER 的类关联出错 自定义View的时候  这个位置只能为空!!! xib关联类应该在view的Class设置,如图 修改了类的关联之后,错误解决.

iOS开发--xib自定义UIView,与IB 、 xib 、 代码 定义与初始化对比

一.自定义UIView 二.关于xib定义的view实例化,初始化. 三.使用IB VS xib  VS 使用代码定义与实例化

用XIB自定义uiView

建立一个类继承自UIView,添加初始化方法 完善初始化方法 XIB文件需要声明所属类 效果图 用XIB自定义uiView

IOS使用XIB自定义UIView

对于复杂的界面,用代码自定义UIView 是一件很痛苦的事情,所以可以用XIB来布局. 大致步骤如下: 1. 在你项目中 新建 类,继承UIView: 2. 在新建个XIB(XIB 的名称要跟新建 类名 一样): 3. 在XIB 中 选中View  改它Class 为你建的 类名: 4. 要使用这个UIView跟平常就不一样了.  因为  不是我们来  实例化它.keyi通过 这个静态方法 来实例 化.在这个静态方法中,传递需要的参数. +(LKTextView *)instanceTextVi

iOS中自定义UIView(用接口获取Lable和TextFile中的值)

NSArray *arrayText = @[@"用户名",@"密码",@"确认密码",@"手机号",@"邮箱"]; NSArray *placeholders = @[@"请输入用户名",@"请输入密码",@"请确认密码",@"请输入手机号",@"请输入邮箱"]; NSInteger y = 30; for

使用代码自定义UIView注意一二三

文/CoderAO(简书作者)原文链接:http://www.jianshu.com/p/68b383b129f9著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 当一撮样式一样的视图在工程中被多次使用的时候,为了方便使用,我们会想把他们抽成一个单独的类,进行视图的自定义. 比如我们要做一个这样的东西: beauty.png 这一块由两个东西组成:一个imageView和一个label.首先我们新建一个继承自UIView的类MyView. 在MyView的.m文件里,你可以根据自己

自定义uiview 当没有数据的时候 显示自定义的uiview界面

// // ZSDTJNoDataView.h // ZSDTJNoDataView // // Created by Mac on 14-12-28. // Copyright (c) 2014年 ZSD. All rights reserved. // //当发起网络请求的时候没有数据的时候界面显示指定的背景图 #import <UIKit/UIKit.h> typedef void(^ZSDNoDataViewCompleteHandler) (NSInteger buttonIndex

自定义UIView以实现自绘

有时候我们需要自绘uiview以实现自己的需求,比如根据坐标点绘制出连续的曲线(股票走势图),就需要自绘uiview了. 原理:继承uiview类(customView),并实现customview的drawRect即可. 首先看一下鲜果图: 代码如下: // .h #import <UIKit/UIKit.h> @interface CustomView : UIView @end //.m #import "CustomView.h" @implementation C

iOS开发总结(A0) - 自定义UIView

根据我浅薄的ios开发经验,可以有以下方法添加custom uiview 的内容 1)draw 2)build in xib 3)add subviews 在custom uiview 的m文件中,一般按照以下对uiview进行初始设置: 1 -(void)awakeFromNib{ 2 [self setup]; 3 } 4 -(void)setup{ 5 //set up view 6 } 7 -(instancetype)initWithFrame:(CGRect)frame{ 8 sel