LoginSignup
7
7

More than 5 years have passed since last update.

【iOS】CustomViewでxibファイルを読込む

Posted at

現在のiOS開発ではStoryboardで画面遷移等も出来るようになって、かなり便利になっています。
開発者には、Storyboardやxibを全く使わないという人が多いみたいですが、
とりあえず、雰囲気だけみてみたい。とかCustomViewをxibでレイアウトしたい。という人は結構多いみたいです。
私の場合は、ソースコードを出来るだけ減らしたい。と考えているタイプなので
Storuboardやxibファイルは積極的に使っています。

なので、UIViewのクラスメソッドとしてxibファイルを読込むメソッドを作りました。
※クラス名と同名のxibファイルが用意されていることが前提です。

+ (id)loadFromNib
{
    LOG_FUNC;

    NSString *nibName = NSStringFromClass([self class]);
    UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    NSArray* arr = [nib instantiateWithOwner:nil options:nil];

    EditorFuncView* view = [arr objectAtIndex:0];
    view.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.85f];//背景色
    return [arr objectAtIndex:0];
}
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7