美文网首页
UIView关联Xib文件并可用AutoLayout

UIView关联Xib文件并可用AutoLayout

作者: moosoul | 来源:发表于2016-04-11 11:33 被阅读0次

UIView关联Xib文件并可用AutoLayout

看了很多教程基本都是复制粘贴,未测试就发出来的,自己测试了一个可以用的写出来

下面的内容可以看可以不看,说说看到的几个复制粘贴出来的文章,导致的问题

  • 通过frame的方式,添加子Viewself上,无法实现当前xib内部subView对父视图的自适应
  • 通过将当前xib里面的最外层view,连接一个IBOutlet的方式,也无法实现自适应

运行环境

  • iOS7.0以上

开发环境

  • Xcode7.2.1 (7.3是个大坑逼,不信你试试自动补全)

解决办法

  1. 创建自定义View

    File -> New -> File

    选择iOS分类下的Source下面的Cocoa Touch Class, 点击Next

    Class输入自定义的View的类名例如CustomView, Subclass of 输入 UIView, 此时下方的Also create XIB file是灰色的无法勾选 (其余的选项自己看吧), 点击Next

    勾选下方的Targets 对应到当前项目(如果未勾选), Create

  2. 创建Xib文件

    File -> New -> File

    选择iOS分类下的User Interface下面的View, 点击Next

    Save As中输入Xib的名字,跟上面的类名一致, 后缀为xib, 例如CustomView.xib (后缀可以不输入)

    勾选下方的Targets 对应到当前项目(如果未勾选), Create

  3. 关联Xib和CustomView类

进入CustomView.xib中, 点击左侧的 File's Owner 选择右边的 Custom Class 输入 CustomView

  1. 自定义CustomView的初始化方法

CustomView.h

  #import <UIKit/UIKit.h>

  @interface CustomView : UIView

  @end

CustomView.m

  #import "CustomView.h"

  @implementation CustomView
  
  - (instancetype)initWithCoder:(NSCoder *)aDecoder
  {
    if (self = [super initWithCoder:aDecoder]) {
          
      // 先添加View
      UIView *view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject;
      [self addSubview:view];
              
      // 再添加约束
      view.translatesAutoresizingMaskIntoConstraints = NO;
      self.translatesAutoresizingMaskIntoConstraints = NO;
      [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
      [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
      [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
      [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    }
  return self;
}

添加完之后

  1. 进入Main.storyboard中添加当前自定义的CustomView
  1. 去自定义的Xib中添加几个视图,测试一波

    上面5中如果在sb中改变上图的CustomView的背景色,是不起作用的,因为在CustomView.xib中被覆盖成默认的whiteColor了

  1. OK测试我们的运行结果

Over 搞定 UIView + XIB + AutoLayout

如果写的不对或者有问题的地方,欢迎大家在评论处指正,相互交流

最后附上自己写的Demo MSBaseCustomView

Demo里面的MSBaseCustomView,可以直接被继承使用

相关文章

  • UIView关联Xib文件并可用AutoLayout

    UIView关联Xib文件并可用AutoLayout 看了很多教程基本都是复制粘贴,未测试就发出来的,自己测试了一...

  • Use Xib in Storyboard / 在StoryBo

    如何使用Xib创建自定义UIView 新建一个空白View xib文件 关联xib文件和code 文件 有两种方式...

  • AutoLayout, Xib, UIView

    不要在 - (void)viewDidLoad; 方法里获取Xib里面的frame值:举例:在一个XIb中含约束...

  • xib创建UIView

    一·创建UIView.h.m文件: 二·创建xib: 三·编辑xib: 1.关联 2.自动布局 3.添加控件 4....

  • iOS自定义UIView

    XIB自定义UIView 在XIB中使用autolayout是非常方便的,所以有的时候我们需要使用XIB来创建自定...

  • swift xib方式自定义view

    1.继承UIView 创建一个CustomView类如图 2.创建一个CustomView.xib文件,关联类,设...

  • 通过Xib创建View

    步骤1.创建一个继承自UIView的view 2.创建Xib文件 3.关联文件 4.运用 bug解决如果 运行报错...

  • xib中关联YYLable

    xib中关联YYLable的控件记住必须为UIView不能是UILable

  • Xib创建自定义View

    1.Command+N创建UIView文件 2.Command+N创建一个和上面UIView文件同名的xib文件 ...

  • Controller和View对象的加载

    1.在xib中获取UIView 当xib的name跟关联的view的类名一致时。 通用的方式 2.在xib中获取U...

网友评论

      本文标题:UIView关联Xib文件并可用AutoLayout

      本文链接:https://www.haomeiwen.com/subject/fxkilttx.html