IOS UI 第五篇:基本UI

时间:2022-11-07 16:50:02

添加个导航栏:

    Xib1 *xib1 = [[Xib1 alloc] initWithNibName:@"Xib1" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:xib1];
    
    
    self.window.rootViewController = navController;
xib1:
- (IBAction)nextPage:(id)sender {
    self.title = @"一";
    Xib2 *xib2= [[Xib2 alloc] initWithNibName:@"Xib2" bundle:nil];
    
    xib2.modalPresentationStyle = UIModalPresentationCustom;
    [self.navigationController pushViewController:xib2 animated:YES];
}
xib2:
- (IBAction)BackfirstPage:(id)sender {
    Xib3 *xib3 = [[Xib3 alloc] initWithNibName:@"Xib3" bundle:nil];
    UIViewController *popVC = self.navigationController.viewControllers[0];
    [self.navigationController pushViewController:xib3 animated:YES];
    popVC.title = @"First Page";
}
xib3:
- (IBAction)BackRoot:(id)sender {
    UIViewController *popVC = self.navigationController.viewControllers[0];
    [self.navigationController popToViewController:popVC animated:YES];
    popVC.title = @"First Page";
    
}
- (IBAction)Back:(id)sender {
    UIViewController *popVC = self.navigationController.viewControllers[1];
    [self.navigationController popViewControllerAnimated:YES];
     popVC.title = @"Second Page";
}
适配导航栏:
第一步:
Supporting Files -> Shipaei67-Info.plist -> View controller-based status bar (拉动第一个栏找这个选项) Value : NO
第二步:
项目->Deployment Info -> Status Bar Style : Black Translucent
做完前两步,导航条上面的字体就有白色变成黑色了
电池栏距离:0-20
导航栏距离:0-44
定义IOS 版本:
#define ISiOS7 ([UIDevice currentDevice].systemVersion.floatValue>=7.0)
关于导航栏的方法:
- (void)viewDidLoad
{
    [super viewDidLoad];
    //判断是否是iOS7
    if (ISiOS7) {
        //在IOS7里,这个是设置导航条文字的颜色
        self.navigationController.navigationBar.tintColor=[UIColor redColor];
        //在IOS7里,这个是设置导航条背景的颜色
        self.navigationController.navigationBar.barTintColor=[UIColor blueColor];

}else{
        //不是ios7,只能设置导航条的颜色
        self.navigationController.navigationBar.tintColor=[UIColor orangeColor];
    }
    //自定义标题
    UILabel *titleLable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    //ios6里,label默认背景是白色,但是ios7默认透明
    if (!ISiOS7) {
        titleLable.backgroundColor=[UIColor clearColor];
    }
    titleLable.text=self.title;
    titleLable.font=[UIFont boldSystemFontOfSize:20];
    titleLable.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=titleLable;
    
    
    //自定义导航条按键(利用系统自带的样式)
    UIBarButtonItem *barButtonItem1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(onBarButtonItemClicked:)];
    UIBarButtonItem *barButtonItem2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onBarButtonItemClicked:)];
    
//    self.navigationItem.rightBarButtonItem=barButtonItem;//单独一个按键
    self.navigationItem.rightBarButtonItems=@[barButtonItem1,barButtonItem2];
    
    
    //自定义导航条按键,完全自定义
    UIButton *leftBarButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftBarButton setTitle:@"左侧按键" forState:UIControlStateNormal];
    [leftBarButton setTitle:@"左侧点击" forState:UIControlStateHighlighted];
    [leftBarButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftBarButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    leftBarButton.frame=CGRectMake(0, 0, 100, 44);
    [leftBarButton addTarget:self action:@selector(onLeftBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //将上面自定义的按键,包装成一个导航条按键
    UIBarButtonItem *leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBarButton];
    self.navigationItem.leftBarButtonItem=leftBarButtonItem;
    
    
    
    //设置导航条背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg"] forBarMetrics:UIBarMetricsDefault];
    
}
-(void)onLeftBarButtonClicked:(id)sender{
    NSLog(@"自定义导航条按键被点击");
}
-(void)onBarButtonItemClicked:(id)sender{
    NSLog(@"导航条按键被点击");
}

通知栏:
main:
@implementation QFAppDelegate{
    Dog *dog;
    Person *person;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    dog=[[Dog alloc]init];
    person=[[Person alloc]init];
    //让人接收通知,把人注册到通知中心中
    [[NSNotificationCenter defaultCenter]addObserver:person selector:@selector(beatThief:) name:@"狗叫:bark!!!" object:Nil];
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeContactAdd];
    button.frame=CGRectMake(100, 100, 50, 50);
    [button addTarget:self action:@selector(makeDogBark:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];

-(void)makeDogBark:(id)sender{
    [dog findThief];
}
Dog.h
#import "Dog.h"

@implementation Dog
-(void)findThief{
    NSString *model=@"测试的数据模型";
    //利用通知中心发通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"狗叫:bark!!!" object:model];
}
@end

Person.m
@implementation Person
-(void)beatThief:(id)sender{
//    NSLog(@"%@",sender);
    NSLog(@"抄家伙,打贼!");
    NSNotification *notice=sender;
    NSString *testModel=notice.object;
    NSLog(@"测试数据的传递:%@",testModel);
}
@end
系统通知:
@implementation QFRegistViewController{
    CGRect buttonOldRect;
    UITapGestureRecognizer *tapGR;//添加单击手势
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    
    /*
     数据传递
     1、首先,数据已经存在了!
     2、谁需要,就声明几个指针。
     3、在实例化2那个对象的时候,将声明的几个指针指向正确是数据。
     4、至此,数据传递完成,可以使用了。
     
     */
    
    //将自己加入通知中心
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradShow:) name:UIKeyboardWillShowNotification object:Nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradHide:) name:UIKeyboardWillHideNotification object:Nil];
    buttonOldRect=self.goDetailButton.frame;
    
    //实例化单击手势
    tapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
    
}
//单击手势的响应函数
-(void)onTap:(id)sender{
    //将输入框取消第一响应者,键盘就睡自动收回
    [self.nameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}
//收到键盘将要弹起的通知后,调用的函数
-(void)onKeyBoradShow:(id)sender{
    [self.view addGestureRecognizer:tapGR];
    NSLog(@"%@",sender);
    //将sender转化成notice类型
    NSNotification *notice=sender;
    //获取通知里面的userinfo
    NSDictionary *userInfo = notice.userInfo;
    //在userinfo中获取键盘最后的状态rect
    id keyBoardRectNotice = userInfo[UIKeyboardFrameEndUserInfoKey];
    //将上面获得的rect对象,提取出cgrect
    CGRect keyBoardRect;
    [keyBoardRectNotice getValue:&keyBoardRect];
    //用获得的cgrect来设置button的frame;
    CGRect buttonNewFrame=buttonOldRect;
    //原来的按键frame减去键盘的高度,生成新的frame
    buttonNewFrame.origin.y-=keyBoardRect.size.height;
    [UIView animateWithDuration:0.2 animations:^{
        self.goDetailButton.frame=buttonNewFrame;
    }];
    
    
}
//收到键盘将要收回的通知后,调用的函数
-(void)onKeyBoradHide:(id)sender{
    [self.view removeGestureRecognizer:tapGR];
    [UIView animateWithDuration:0.3 animations:^{
        self.goDetailButton.frame=buttonOldRect;
    }];
}
//按键函数
- (IBAction)goDetail:(id)sender {
    NSString *name=self.nameTextField.text;
    NSString *password=self.passwordTextField.text;
    
    //生成数据模型
    QFUserModel *model=[[QFUserModel alloc]init];
    model.name=name;
    model.pass=password;
    
    QFDetailViewController *detailVC=[[QFDetailViewController alloc]initWithNibName:@"QFDetailViewController" bundle:Nil];
    //将数据模型传入
    detailVC.model=model;
    
    [self presentViewController:detailVC animated:YES completion:^{
        
    }];
    
}

//销毁函数,用于移除通知
-(void)dealloc{
    //在销毁的时候将自己在通知中心中移除
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end