IOS XIBiOS开发程序员

[iOS UI] 如何通过xib创建自定义UIView并在xib

2016-04-13  本文已影响8200人  段子周

需求描述

因为我的项目正处于UI设计还没有定稿,变化的可能性较大阶段,当然要使用autolayout,所以我想要定制UIView并从xib中加载界面,而且在UIViewController的xib文件中使用前面的定制UIView,各种google,SO,多少不满足需求或者有错误,所以记录一篇。

步骤

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.rootViewController = [[UIViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    [_window makeKeyAndVisible];
    
    return YES;
}

运行,确认程序已经加载你自定义的xib而不是走默认的storyboard

#import "CustomizedView.h"
@implementation CustomizedView
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    if (self = [super initWithCoder:aDecoder]) {
        UIView *view = [[[NSBundle mainBundle]loadNibNamed:@"CustomizedView" owner:self options:nil]objectAtIndex:0];
        [self addSubview:view];
    }
    return self;
}
@end
上一篇 下一篇

猜你喜欢

热点阅读