forked from mixi-inc/iOSTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
3.2 UIView のカスタマイズ
tamotamago edited this page Apr 22, 2013
·
9 revisions
UIKit で提供されている view component を組み合わせてカスタムビューを作成すれば、あらゆる場面で再利用可能になり開発効率があがります。
ここではカスタムビューコンポーネントの作成方法として xib を使用した方法と使用しない方法の二つを紹介します。
MixiCustomizedView.h
#import <UIKit/UIKit.h>
@interface MixiCustomizedView : UIView
@end
MixiCustomizedView.m
#import "MixiCustomizedView.h"
@implementation MixiCustomizedView
static CGRect const kSubViewFrame = {{10, 10}, {300, 180}};
static CGRect const kButtonFrame = {{10, 10}, {220, 30}};
// コードで初期化する場合
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self initializeView];
}
return self;
}
// xib を使用する場合
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
[self initializeView];
}
return self;
}
-(void)initializeView
{
UIView *subView = [[UIView alloc] initWithFrame:kSubViewFrame];
[subView setBackgroundColor:[UIColor redColor]];
[self addSubview:subView];
UILabel *label = [[UILabel alloc] initWithFrame:kButtonFrame];
label.text = @"hogehoge";
label.backgroundColor = [UIColor clearColor];
[subView addSubview:label];
}
@end
NSBundle UIKit Additions Reference
MixiCustomizedView2.m
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
//xib ファイルのトップレベルオブジェクトを格納した array を返す
NSArray *topLevelViews = [[NSBundle mainBundle] loadNibNamed:@"MixiCustomizedView2"
owner:self
options:nil];
UIView* customizedView2 = topLevelViews[0];
[self addSubview:customizedView2];
}
return self;
}
xib の owner を設定することで、ボタンアクションなどのひも付けも可能になります。
MixiCustomizedView.xib
- 導入
- Objective C の基礎
- メモリ管理
- UIViewController1 - UIViewController のカスタマイズ
- UIViewController2 - ModalViewController
- UIViewController3 - ライフサイクル
- UIKit 1 - container, rotate-
- UINavigationController
- UITabController
- Custom Container View Controller
- Supporting Multiple Interface Orientations
- UIKit 2- UIView -
- UIView
- UIView のカスタマイズ
- UIView Animation
- UIKit 3 - table view -
- UITableView について
- UITableViewとNavigationController
- custom UITableViewCell の作成
- 4.4 UITableViewのその他のオプション、カスタマイズ
- UIKit 4 - images -
- UIImage (CoreGraphics)
- UIImageView
- Accets Library
- CoreImage
- UIKit 4 - text -
- UILabel
- UITextView
- KeybordNotification
- 非同期処理系
- NSURLConnection (json シリアライザ)
- Blocks
- GCD
- ローカルキャッシュ
- UserDefaults
- FileManager
- CoreData
- SQLite
- Instruments
- leak
- time profiler
- その他
- 単体テスト (GHUnit)
- 結合テスト (KIF)
- cocoaPods でライブラリ管理