원문1 : http://lambert.tistory.com/355
디자인 적용을 위해 커스텀 컨트롤(Custom Control)을 적용하기 위해서 다음과 같이 세 가지 방법을 사용할 수 있다. 다음의 예는 최대한 간단한 설명을 위해 Naviation Control을 대상으로 하고, Xcode의 Navigation-based Application을 사용했다.
#import <UIKit/UIKit.h>
@interface CustomNavigationBar : UINavigationBar <UINavigationControllerDelegate> {
}
@end
#import "CustomNavigationBar.h"
@implementation CustomNavigationBar
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
// 네비게이션바에 배경 이미지 추가.
UIImage *image = [[UIImage imageNamed:@"NavigationBarBackground.png"]retain];
[image drawInRect:rect];
[image release];
}
- (void)dealloc {
[super dealloc];
}
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomBar)
@end
#import "UINavigationBar+CustomBar.h"
@implementation UINavigationBar (CustomBar)
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
// 네비게이션바에 배경 이미지 추가.
UIImage *image = [[UIImage imageNamed:@"NavigationBarBackground.png"] retain];
[image drawInRect:rect];
[image release];
}
- (void)dealloc {
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 네비게이션바에 배경 이미지 추가.
UIImage *image = [UIImage imageNamed:@"NavigationBarBackground.png"];
UIImageView *naviBarImageView = [[UIImageView alloc] initWithImage:image];
naviBarImageView.frame = CGRectMake(0, 20,
image.size.width, image.size.height);
[self.navigationController.view addSubview:naviBarImageView];
[naviBarImageView release];
}
아이폰이나 안드로이드 프로젝트를 하다보면 처음에 가장 신경쓰이는 부분이 UI구성입니다.
1. 코드를 이용해 UINavigationController 만들기
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
}
{
[super viewDidLoad];
}
3.Navigation Bar 숨기기 & 보이기
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES; //YES : 숨기기, NO : 보이기
}
다시 보이게 하려면 self.navigationController.navigationBarHidden값을 NO로 하시면 되겠습니다.
4. Navigation Bar 배경색 바꾸기 (Background Color)
{
[super viewDidLoad];
self.title = @"My Title";
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255.0/255
green:10.0/255 blue:100.0/255 alpha:1];
}
5. Navigation Bar Style 바꾸기
{
super viewDidLoad];
self.title = @"My Title";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack; //스타일 적용
self.navigationController.navigationBar.translucent = YES; // 반투명 효과 주기
}
6. Navigation Bar 배경이미지 넣기 (Background image)
{
UIImage *bgImage = [UIImage imageNamed:@"background.png"];
[bgImage drawInRect:rect];
}
@end
7. Navigation Bar Back 버튼의 Text 변경 하기
{
[super viewDidLoad];
self.navigationController.navigationBar.backItem.title = @"뒤로가기";
}
위 코드는 상황에 따라 적용되지 않을 수 있습니다.
가령 UITabbarController와 UINavigationController를 같이 사용하면서 pushViewController를 이용해 뷰를 이동할 때 안되더군요.
이때는 다음과 같이 해결 합니다.
initWithTitle: @"Back"
style: UIBarButtonItemStyleBordered
target:nil action:nil];
[[self navigationItem] setBackBarButtonItem: backButton];
UINavigationController *homeNav = self.navigationController;
LoginView * loginView = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
[homeNav pushViewController:loginView animated:YES];
즉 뷰를 이동하고 나서 처리하는 것이 아니라 이동하기 전에 백버튼을 설정하고 이동하는 처리 입니다.
여기서 하나 더 알아봅시다.
NavigationController에서 화면 전환시 이전 화면으로 이동하고자 할 때 바로 이전 화면이 아닌 자신이 원하는 이전 화면으로 이동하고 싶을 때 처리는 어떻게 할까요.
다음을 보시죠
8.Navigation Bar 오른쪽의 버튼 아이템 추가하기
{
[super viewDidLoad];
style:UIBarButtonItemStylePlain target:self
action:@selecter(ActLogin)];
self.navigationItem.rightBarButtonItem = loginButtonItem;
[loginButtonItem release];
}
//버튼을 눌렀을 때 이벤트 처리
-(IBAction) ActLogin:(id)sender
{
}
버튼의 종류나 여러가지 옵션은 상황에 맞게 처리하시면 되겠습니다.
일단 NavigationController 커스터마이징은 여기까지 정리해 놓겠습니다.
==================================================
배경 이미지 넣기 5.0 이상 코드에서는 다음과 같이 사용
if ([_navController.navigationBar respondsToSelector :@selector(setBackgroundImage:forBarMetrics:)]){
NSLog(@"iPhone 5.0 이상");
UIImage *bgImage = [UIImage imageNamed:@"sw01_a01_bg_top_2.png"];
[_navController.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
}
'모바일개발(Mobile Dev) > IOS개발(ObjectC)' 카테고리의 다른 글
iOS7에서 iOS6.1.3으로 다운그레이드 하기 - ifaith (아이튠즈 에러 11번 fix) (0) | 2015.01.17 |
---|---|
[iOS 앱 만들기 001] 앱 델리게이트 (0) | 2015.01.14 |
iOS 디바이스별 화면 구성요소 치수들 (0) | 2015.01.14 |
Xcode 단축키 정리 (0) | 2015.01.14 |
Xcode와 GitHub 연동하기 (0) | 2015.01.14 |