iOS 응용 프로그램 에서 Toolbar 도구 모음 방식 으로 보 기 를 전환 하 는 방법 에 대한 자세 한 설명

11765 단어 iOSToolbar
UIToolbar 에 대하 여
도구 모음 도구 모음 은 보기 View 의 속성 으로 도구 모음 단추 인 Bar Button Item(사용자 정의 Custom 일 수도 있 고 시스템 자체 의 BarButton System Item 일 수도 있 음)을 추가 할 수 있 으 며,보기 컨트롤 러 는 도구 모음 항목 을 통 해 보기 의 내용 을 조작 할 수 있 습 니 다.
주의사항:
네 비게 이 션 표시 줄 컨트롤 러 에 UIToolBar 인 스 턴 스 가 있 지만 기본 값 은 숨겨 져 있 습 니 다.표시 하려 면 이 방법 을 통 해 열 어야 합 니 다.
201642193049007.png (766×89)
여기 서 주의해 야 할 것 은 UINavigation Bar 와 유사 합 니 다.네 비게 이 션 컨트롤 러 는 하나의 UIToolBar 인 스 턴 스 만 가지 고 있 지만 UIToolBar 가 가지 고 있 는 UIBarButton Item 인 스 턴 스 는 보기 컨트롤 러 에 의 해 관리 되 고 있 습 니 다.다음 과 같 습 니 다.
201642193128090.png (757×159)
도구 모음 스타일:

typedef NS_ENUM(NSInteger, UIBarStyle) {
  UIBarStyleDefault     = 0,    //    ,    
  UIBarStyleBlack      = 1,    //    ,    
  UIBarStyleBlackOpaque   = 1,  //      ,    
  UIBarStyleBlackTranslucent = 2,  //       ,    
};

속성:

@property(nonatomic)    UIBarStyle barStyle;  //     ,     
@property(nonatomic,copy)  NSArray  *items;   //         ,UIBarButtonItem
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent //    
@property(nonatomic,retain) UIColor *tintColor;    //    
@property(nonatomic,retain) UIColor *barTintColor; //     
방법:
※도구 모음 에 있 는 단추 셀 설정

- (void)setItems:(NSArray *)items animated:(BOOL)animated; 
※도구 모음 배경 그림 설정

- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;

※도구 모음 배경 그림 가 져 오기

- (UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
※도구 막대 그림자 그림 설정

- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;
 ※도구 막대 의 그림자 그림 가 져 오기

- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom ;
도구 막대 방식 보기 전환
1.프로젝트 생 성:
Xcode 를 실행 하고 새 Empty Application 을 만 듭 니 다.이름 은 MultiView 이 고 다른 설정 은 다음 그림 과 같 습 니 다.
201642193318520.png (517×221)
2.View Controller 3 개 만 들 기:
순서대로 File―New―New File 을 선택 하고 다음 창 을 엽 니 다.
201642193334536.png (734×494)
UIViewController subclass 를 찾 고 Next 를 누 르 면 아래 창 을 엽 니 다.
201642193358623.png (594×321)
이름 RootViewController 를 입력 하고 Subclass of 가 UIViewController 를 선택 할 것 을 보증 합 니 다.아래 두 개의 선택 상 자 는 선택 하지 않 습 니 다.같은 절차 에 따라 두 개의 View Controller 를 새로 만 들 었 는데 이름 은 각각 First View Controller 와 SecondView Controller 입 니 다.만 든 후 Project Navigation 에 다음 과 같은 파일 을 표시 합 니 다.
201642193415059.png (263×271)
3.View Controller 세 개 를 위 한.xib 파일 만 들 기:
순서대로 File―New―New File 을 선택 하고 다음 창 을 엽 니 다.
201642193435862.png (733×494)
왼쪽 에서 User Interface 를 선택 하고 오른쪽 에서 View 를 선택 하 며 Next 를 누 르 고 새 창 에 있 는 Device Family 에서 iPhone 을 선택 하고 Next 를 누 르 면 다음 창 을 엽 니 다.
201642193455889.png (429×311)
이름 RootView 를 입력 하고 Create 를 누 르 면.xib 파일 을 만 듭 니 다.같은 방법 으로 두 개의 xib 를 만 듭 니 다.이름 은 각각 First View 와 SecondView 입 니 다.
4.앱 위임 수정:
4.1 AppDelegate.h 를 누 르 면 코드 를 추가 하고@interface 전에@class RootViewController 를 추가 합 니 다.@end 전에@property(strong,nonatomic)RootViewController*rootViewController 를 추가 합 니 다.추 가 된 코드 는 다음 과 같 습 니 다.

#import <UIKit/UIKit.h>
@class RootViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) RootViewController *rootViewController;
@end
4.2 AppDelegate.m 를 누 르 면 코드 를 수정 합 니 다.@implementation 에 앞서\#import"RootViewController.h"를 추가 하고@implementation 에@synthesize rootViewController 를 추가 합 니 다.그리고 didFinishLaunching WithOptions 방법 을 다음 과 같이 수정 합 니 다.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  // Override point for customization after application launch.
  self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil]; 
  UIView *rootView = self.rootViewController.view; 
  CGRect rootViewFrame = rootView.frame; 
  rootViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height; 
  rootView.frame = rootViewFrame; 
  [self.window addSubview:rootView]; 
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  return YES;
}
 self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil]; 
이 코드 는 RootView.xib 파일 에서 rootView Controller 를 초기 화 하 는 데 사 용 됩 니 다.initWithNib Name:@"RootView"에서 접미사 이름 을 사용 하지 마 십시오.xib
② 
rootViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height; 
루트 뷰 컨트롤 러 의 보기 가 상태 표시 줄 에 가 려 지지 않도록 합 니 다.
5.RootViewController.h 수정:
루트 뷰 컨트롤 러.h 를 누 르 면 두 개의 속성 과 방법 을 추가 합 니 다.다음 과 같 습 니 다.

#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController;
@interface RootViewController : UIViewController
@property (strong, nonatomic) FirstViewController *firstViewController;
@property (strong, nonatomic) SecondViewController *secondViewController;
- (IBAction)switchViews:(id)sender;
@end
6.RootView.xib 를 열 고 앉 으 면서 File's Owner 를 선택 하고 오른쪽 에서 Identity Inspector 를 열 고 Class 드 롭 다운 메뉴 에서 RootView Controller 를 선택 합 니 다.
이렇게 하면 우 리 는201642193602292.png (714×197)RootView.xib 파일 에서 RootView Controller 에 Outlet 과 Action 을 만 들 수 있 습 니 다.
7.RootView.xib 에 도구 모음 추가:RootView.xib 를 열 고 Tool Bar 를 보기 로 끌 고 Tool Bar 의 단 추 를 두 번 누 르 면 Switch Views 라 는 이름 을 변경 합 니 다.
201642193620664.png (421×229)
8.Action 맵 추가:
Switch Views 단 추 를 선택 하고 Control 을 누 르 고 File's Owner 로 끌 고 가서 마 우 스 를 풀 고 switch Views 방법 을 선택 하 십시오.
201642193655682.png (269×202)
9.File's Owner 를 선택 하고 Control 키 를 누 르 고 View 로 끌 고 마 우 스 를 놓 고 view 를 선택 합 니 다.
201642193709607.png (235×193)
10.RootViewController.m 수정:
RootViewController.m 파일 을 열 고@implementation 전에 코드 를 추가 합 니 다.

#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation 이후 코드 추가:

@synthesize firstViewController;
@synthesize secondViewController;
다음은 view DidLoad 방법 을 수정 합 니 다.이 방법 은 기본적으로 주석 이 지 워 졌 습 니 다.먼저 주위 의 주석 부 호 를 제거 한 다음 코드 를 다음 과 같이 수정 합 니 다.

- (void)viewDidLoad
{
  self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
  [self.view insertSubview: firstViewController.view atIndex:0];
  [super viewDidLoad];
}
switchViews 추가 방법:

- (IBAction)switchViews:(id)sender {
  if (self.secondViewController.view.superview == nil) {
    if (self.secondViewController == nil) { 
      self.secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil]; 
    } 
    [firstViewController.view removeFromSuperview]; 
    [self.view insertSubview:self.secondViewController.view atIndex:0]; 
  } else {
    if (self.firstViewController == nil) { 
      self.firstViewController = 
      [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil]; 
    } 
    [secondViewController.view removeFromSuperview]; 
    [self.view insertSubview:self.firstViewController.view atIndex:0]; 
  } 
}
didReceiveMemory 경고 방법 수정:

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  if (self.firstViewController.view.superview == nil) { 
    self.firstViewController = nil; 
  } else { 
    self.secondViewController = nil; 
  } 
}
11.First View.xib 파일 을 열 고 왼쪽 에 있 는 File's Owner 를 선택 한 다음 Identity Inspector 에서 Class 를 First View Controller 로 선택 합 니 다.그리고 Control 키 를 누 르 고 File's Owner 아이콘 에서 View 로 끌 어 다 놓 고 팝 업 메뉴 에서 view 를 선택 하 십시오.SecondView.xib 를 위해 같은 동작 을 하지만 Class 는 SecondView Controller 로 선택 합 니 다.
12.First View.xib 파일 을 열 고 View 를 선택 하고 Attribute Inspector 를 열 어 다음 과 같은 설정 을 합 니 다.
201642193814931.png (900×468)
SecondView.xib 를 똑 같이 설정 하지만 배경 색 은 빨간색 으로 설정 합 니 다.
13.이 때 프로그램 을 실행 하면 시작 할 때 프로그램 이 보 여 주 는 녹색 배경 을 볼 수 있 습 니 다.스위치 뷰 단 추 를 가볍게 누 르 면 배경 이 빨간색 으로 변 합 니 다.버튼 을 계속 가볍게 누 르 면 배경 이 계속 바 뀝 니 다.
14.배경 전환 애니메이션 효과 추가:
RootView Controller.m 를 열 고 switchViews 를 수정 하 는 방법 은 다음 과 같 습 니 다.

- (IBAction)switchViews:(id)sender { 
  [UIView beginAnimations:@"View Flip" context:nil]; 
  [UIView setAnimationDuration:1.25]; 
  [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  if (self.secondViewController.view.superview == nil) { 
    if (self.secondViewController == nil) { 
      self.secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil]; 
    } 
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 
    [self.firstViewController.view removeFromSuperview]; 
    [self.view insertSubview:self.secondViewController.view atIndex:0]; 
  } else { 
    if (self.firstViewController == nil) { 
      self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil]; 
    } 
    [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 
    [self.secondViewController.view removeFromSuperview]; 
    [self.view insertSubview:self.firstViewController.view atIndex:0]; 
  } 
  [UIView commitAnimations]; 
}
전환 효 과 를 나타 내 는 네 개의 상수 에 주의 하 십시오.

UIViewAnimationTransitionFlipFromLeft
UIViewAnimationTransitionFlipFromRight
UIViewAnimationTransitionCurlDown
UIViewAnimationTransitionCurlUp
각각 왼쪽 에서 뒤 집기,오른쪽 에서 뒤 집기,아래로 말 기,위로 말 기 를 나타 낸다.
실행 후 페이지 넘 기기 효 과 는 다음 과 같 습 니 다.
201642193904648.png (350×500) 201642193920985.png (350×500)

좋은 웹페이지 즐겨찾기