IOS入门-页面控制

页面控制

    • UINavigationController(导航控制器)
      • 添加根视图
      • 使用UINavigationController实现跳转
    • UITabBarController(选项卡导航器)
      • 将UINavigationController放到UITabController中
    • 页面跳转
      • 使用presenter进行跳转
      • 使用dismiss销毁和push进行跳转
      • 使用pop返回上一页
    • 页面布局讲解
    • 横竖屏控制
      • 给UIDevice添加扩展
      • 使用UIdevice实现转屏
    • UIDevice和UIScreen
    • 手势

UINavigationController(导航控制器)

添加根视图

//
//  ViewController.m
//  NavigationController
#import "ViewController.h"
#import "FirstViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //容器
    self.view.backgroundColor = [UIColor redColor];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    FirstViewController *vc = [[FirstViewController alloc]init];
    UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
    [nc.navigationBar setTranslucent:NO];
    [self presentViewController:nc animated:YES completion:^{
        
    }];
    
    
}
@end

使用UINavigationController实现跳转

//
//  FirstViewController.m
//  NavigationController
//
//  Created by clz on 2019/9/8.
//  Copyright © 2019 clz. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.title = @"First";
  UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonSystemItemEdit target:self action:@selector(edit:)];
    
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"自定义的编辑" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [editButton setBackgroundColor:[UIColor yellowColor]];
    [editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:editButton];
    
    self.navigationItem.rightBarButtonItem = item1;
}

- (void)edit:(UIBarButtonItem *)sender{
    SecondViewController *secondViewC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondViewC animated:YES];
}
@end

UITabBarController(选项卡导航器)

将UINavigationController放到UITabController中

//
//  ViewController.m
//  UITabBarController
//
//  Created by clz on 2019/9/8.
//  Copyright © 2019 clz. All rights reserved.
//

#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //tabbarController  最大
    //navigationControlelr
    //里面放的是viewcontroller
    FirstViewController *first = [[FirstViewController alloc]init];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:first];
    [nav1.navigationBar setTranslucent:NO];
    nav1.title = @"First";
    
    UIImage *nav1DefImage = [UIImage imageNamed:@"home_discovergray"];
    nav1DefImage = [nav1DefImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIImage *nav1SelectedImage = [UIImage imageNamed:@"home_discovegreen"];
    nav1SelectedImage = [nav1SelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    nav1.tabBarItem.image = nav1DefImage;
    nav1.tabBarItem.selectedImage = nav1SelectedImage;
    
    
    
    SecondViewController *second = [[SecondViewController alloc]init];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:second];
    nav2.title = @"Second";
    nav2.tabBarItem.image = [UIImage imageNamed:@"home_personalgray"];
    nav2.tabBarItem.selectedImage = [UIImage imageNamed:@"home_personalgreen"];

    ThirdViewController *third = [[ThirdViewController alloc]init];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:third];
    nav3.title = @"Third";
    nav3.tabBarItem.image = [UIImage imageNamed:@"home_studygray"];
    nav3.tabBarItem.selectedImage = [UIImage imageNamed:@"home_studygreen"];

    
    
    UITabBarController *tabBarControlelr = [[UITabBarController alloc]init];
    tabBarControlelr.viewControllers = @[nav1,nav2,nav3];
    tabBarControlelr.tabBar.translucent = NO;
    
    [[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
    
    [[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];

    
    [self presentViewController:tabBarControlelr animated:YES completion:^{
        
    }];
   
}


@end

页面跳转

使用presenter进行跳转

//  ViewController.m
//  页面的跳转
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    /*
     push与present都可以推出新的界面。
     present与dismiss对应,push和pop对应。
     present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他vc。
     present一般用于不同业务界面的切换,push一般用于同一业务不同界面之间的切换
     */
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    FirstViewController *first = [[FirstViewController alloc]init];
    UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:first];
    
    [self presentViewController:nv animated:YES completion:^{
        
    }];
    
}
@end

使用dismiss销毁和push进行跳转

//  FirstViewController.m
//  页面的跳转
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.title = @"First";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.frame = CGRectMake(0, 100, 100, 40);
    [button setBackgroundColor:[UIColor whiteColor]];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"跳转" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    button1.frame = CGRectMake(0, 200, 100, 40);
    [button1 setBackgroundColor:[UIColor whiteColor]];
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
}

- (void)back:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)push:(UIButton *)sender{
    
    SecondViewController *second = [[SecondViewController alloc]init];
    
    [self.navigationController pushViewController:second animated:YES];
    
}

@end

使用pop返回上一页

//  SecondViewController.m
//  页面的跳转

#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"Second";
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"返回" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    button1.frame = CGRectMake(0, 200, 100, 40);
    [button1 setBackgroundColor:[UIColor whiteColor]];
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"跳转" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    button2.frame = CGRectMake(0, 100, 100, 40);
    [button2 setBackgroundColor:[UIColor whiteColor]];
    [button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

}
- (void)back:(UIButton *)sender{
//    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)push:(UIButton *)sender{
    ThirdViewController *third = [[ThirdViewController alloc]init];
    [self.navigationController pushViewController:third animated:YES];
}

@end

页面布局讲解

//  FirstViewController.m
//  页面布局讲解
#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor yellowColor];
    
    
    NSLog(@"宽2 = %f;高2 = %f",self.view.frame.size.width,self.view.frame.size.height);
    
    NSLog(@"X2 = %f;Y2 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0,64, 100, 100);
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];

}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    NSLog(@"宽3 = %f;高3 = %f",self.view.frame.size.width,self.view.frame.size.height);
    
    NSLog(@"X3 = %f;Y3 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
}



@end

横竖屏控制

给UIDevice添加扩展

//  ViewController.m
//  横竖屏的控制
#import "ViewController.h"
#import "UIDevice+Direction.h"
#import "AppDelegate.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"旋转" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
    [button setBackgroundColor:[UIColor redColor]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)action:(UIButton *)sender{
    ((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
    [UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
}


@end

使用UIdevice实现转屏

//  ViewController.m
//  横竖屏的控制

#import "ViewController.h"
#import "UIDevice+Direction.h"
#import "AppDelegate.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"旋转" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
    [button setBackgroundColor:[UIColor redColor]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)action:(UIButton *)sender{
    ((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
    [UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
}


@end

UIDevice和UIScreen

//  ViewController.m
//  UIDevice和UIScreen
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UIDevice
    UIDevice *currentDevice = [UIDevice currentDevice];
    NSLog(@"%@",currentDevice.name);
    NSLog(@"%@",currentDevice.model);
    NSLog(@"%@",currentDevice.localizedModel);
    NSLog(@"%@",currentDevice.systemName);
    NSLog(@"%@",currentDevice.systemVersion);
    NSLog(@"%@",currentDevice.identifierForVendor);
    NSLog(@"%d",currentDevice.multitaskingSupported);
    NSLog(@"%ld",(long)currentDevice.userInterfaceIdiom);//设备交互界面类型
    
    //UIScreen
    UIScreen *screen =[UIScreen mainScreen];
    NSLog(@"修改屏幕亮度前 = %f",screen.brightness);
    screen.brightness = 1;
    NSLog(@"修改屏幕亮度后 = %f",screen.brightness);
    NSLog(@"屏幕宽度 = %f",screen.bounds.size.width);
    NSLog(@"屏幕高度 = %f",screen.bounds.size.height);
}


@end

手势

//
//  ViewController.m
//  点击手势
//
//  Created by clz on 2019/9/15.
//  Copyright © 2019 clz. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"testImage"];
    [self.view addSubview:imageView];
    imageView.frame = CGRectMake(0, 200, 100, 100);
//    imageView.bounds = CGRectMake(0, 0, 100, 100);
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    
    //点击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired  = 1;
    [imageView addGestureRecognizer:tapGesture];
    
      [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    //轻扫
//    self.view
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
    
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:leftSwipe];
    [self.view addGestureRecognizer:rightSwipe];

//长按手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick:)];
    longPressGesture.minimumPressDuration = 1;//长按时间
    [imageView addGestureRecognizer:longPressGesture];
}

- (void)tapClick:(UITapGestureRecognizer *)sender{
    NSLog(@"触发");
	}
	
- (void)swipeClick:(UISwipeGestureRecognizer *)sender{
    //用方向来区分
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"左滑动");
    }else{
        NSLog(@"右滑动");
    }

- (void)longPressClick:(UILongPressGestureRecognizer *)sender{
    //判断状态,否则会触发多次
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"触发");

    }
}




@end

你可能感兴趣的:(IOS入门-页面控制)