UINavigationController 연습
4284 단어 iOSUINavigationBar
#import "AppDelegate.h"
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue;
if (sysVersion>=8.0) {
UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:setting];
}
UIWindow* w = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
w.backgroundColor = [UIColor whiteColor];
self.window = w;
RootViewController* r =[[RootViewController alloc] init];
UINavigationController* navBar = [[UINavigationController alloc] initWithRootViewController:r];
w.rootViewController = navBar;
[w makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
#import "SecondViewController.h"
@implementation RootViewController
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
[self.navigationController setToolbarHidden:NO animated:YES];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(20, 64, 80, 40)];
[btn setBackgroundColor:[UIColor brownColor]];
[btn setTitle:@"添加" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIBarButtonItem* BarBtn = [[UIBarButtonItem alloc] initWithTitle:@"随机背景" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
self.navigationItem.leftBarButtonItem = BarBtn;
@try {
UINavigationItem* item = self.navigationController.navigationBar.items[0];
NSLog(@"%p",self.navigationItem);
NSLog(@"%p",item);
item.title = @"HelloWorld";
}
@catch (NSException *exception) {
NSLog(@"%@",exception.description);
}
@finally {
}
}
-(void)onClick:(UIBarButtonItem*) ABarBtn
{
CGFloat r = ( arc4random() % 256 / 256.0 );
CGFloat g = ( arc4random() % 256 / 256.0 );
CGFloat b = ( arc4random() % 256 / 256.0 );
UIColor* c2 = [UIColor colorWithRed:r green:g blue:b alpha:1];
self.view.backgroundColor = c2;
}
-(void)BtnClicked:(UIButton*)abtn
{
SecondViewController* sec = [[SecondViewController alloc] init];
[self.navigationController pushViewController:sec animated:NO];
}
@end
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
#import "SecondViewController.h"
@implementation SecondViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
}
-(void)viewWillAppear:(BOOL)animated
{
UINavigationItem* item = self.navigationController.navigationBar.items[1];
[self.navigationController setToolbarHidden:YES animated:YES];
item.title = @"HelloWorldSec";
}
@end
이 UI 구성 요소 사용에 대한 개인적인 이해1. UINavigationController는 UIViewController에서 상속되지만 UIViewController는 탐색에 액세스하는 속성을 유지하므로 해당 인스턴스의 통신이 용이합니다.
2. UIViewController에서 상속받고 싶다면 직접 상속하는 것이 좋고, UITabViewController를 상속하면 UINavigationController 속성도 가지고 있지만 접근할 수 없고 nil이다.
3, UINavigationController는 스택 관리의 방법입니다.
4. UINavigationController는 인스턴스 별로 UINavigationItem을 생성하며 self.navigationItem을 통한 접근은 self.navigationController.navigationBar.item[0]과 동일합니다.
5. NavigationBar의 설정은 어떤 ViewController가 표시되든 전역적입니다.
6. 이 NavigationBar는 Controller의 View 좌표와 겹칩니다.새로 서브뷰를 생성하여 표시하고 싶다면 NavigationBar의 기본 높이(44)를 고려해야 합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
View의 레이아웃 방법을 AutoLayout에서 따뜻한 손 계산으로 하면 성능이 9.26배로 된 이야기이 기사는 의 15 일째 기사입니다. 어제는 에서 이었습니다. 손 계산을 권하는 의도는 없고, 특수한 상황하에서 계측한 내용입니다 화면 높이의 10 배 정도의 contentView가있는 UIScrollView 레이아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.