UI(1)

1、UIView

1.1 介绍

UIView表示屏幕上的一块矩形区域,它在App中占有绝对重要的地位,因为IOS中几乎所有可视化控件都是UIView的子类。负责渲染区域的内容,并且响应该区域内发生的触摸事件。

UIView的功能 1.管理矩形区域里的内容2.处理矩形区域中的事件3.子视图的管理 4.还能实现动画

UIView的子类也具有这些功能

img

1.2 语法

// x y width height
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 150, 150)];
// 背景色
view1.backgroundColor = [UIColor redColor];

// 设置旋转45度的形变属性
view1.transform = CGAffineTransformRotate(view1.transform, M_PI_4);

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 150, 150)];
view2.backgroundColor = [UIColor greenColor];

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 150, 150)];
view3.backgroundColor = [UIColor blueColor];

// 自视图的层级关系会按照添加的顺序,从下至上依次排列,先加入的View会被后来加入的View盖住
// 若想改变顺序,调整下面三行代码的先后顺序即可
[self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];
// 删除view
view1.removeFromSuperview;
// 移动指定的子视图,使其显示在其所以兄弟节点之上
[self.view bringSubviewToFront:view2];
// 移动指定的子视图,使其显示在其所有兄弟节点之下
[self.view sendSubviewToBack:view3];

2、UIButton

2.1 介绍

  1. IButton,俗称“按钮”
  2. 一般情况下,点击某个控件后,会做出相应反应的都是按钮
  3. 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置

2.2 语法

常用属性:

// 2. 设置位置尺寸
button.frame = CGRectMake(100, 200, 200, 50);

// 3. 设置文字
// 3.1 常态的文字
[button setTitle:@"我是按钮" forState:UIControlStateNormal];
// 3.2 高亮状态的文字
[button setTitle:@"我是高亮按钮" forState:UIControlStateHighlighted];

// 4. 设置字体颜色
// 4.1 常态的字体颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
// 4.2 高亮状态的字体颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

// 4. 设置图片
// 4.1 常态下的图片
[button setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
// 4.2 高亮下的图片
[button setImage:[UIImage imageNamed:@"like_pressed"] forState:UIControlStateHighlighted];

// 5. 设置背景图片
// 5.1 常态下的背景图
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
// 5.2 高亮下的背景图
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_hightlighted"] forState:UIControlStateHighlighted];

// 6. 设置按钮点击状态, 默认就是YES ,NO 状态是不可点击
button.enabled = YES;

// 7. 将按钮添加入控制器view中
[self.view addSubview:button];

demo:

//按钮事件
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(200, 750, 50, 50)];
//设置标题
[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];
btn1.backgroundColor = [UIColor blueColor];
//点击后触发@selector里面的方法
[btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
//删除事件
//[button1 removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

- (void) btnAction:(id)sender {
    UIButton* btn = (UIButton*)sender;
    NSLog(@"hello");
}

3、UITextField

3.1 介绍

UITextField 是一个用来处理文本输入和现实的控件,在我们的开发当中也是经常被用到。

3.2 语法

- (void)addTextField {
    // 初始化
    UITextField *textField = [[UITextField alloc]  initWithFrame:CGRectMake(50, 50, 100, 20)];
    // 设置TextField的边框
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    // 设置TextField的占位字符
    textField.placeholder = @"请输入";
    // textField.text = @"点击试试";
    // 设置开始编辑时是否删除原有内容
    // textField.clearsOnBeginEditing = YES;
    // 设置编辑框中删除按钮的出现模式 1.没有删除按钮:UITextFieldViewModeNever 2.当编辑的时候出现删除按钮:UITextFieldViewModeWhileEditing 3.当不编辑的时候出现删除按钮:UITextFieldViewModeUnlessEditing 4.总是出现:UITextFieldViewModeAlways
    textField.clearButtonMode = UITextFieldViewModeAlways;
   
    [self.view addSubview:textField];
}

4、UILabel

4.1 介绍

标签用于显示静态内容,包括单独的一行或多行。

4.2 语法

- (void) addLabel {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    // 限制在N行内自适应
    label.numberOfLines = 3;
    label.text = @"please enter";
    label.textColor = [UIColor redColor];
    // 字体居中
    label.textAlignment = NSTextAlignmentCenter;
    // label.backgroundColor = [UIColor blueColor];
    [self.view addSubview:label];
}

5、UIImageView

5.1 介绍

图像视图用于显示单个图像或动画序列的图像。

5.2 语法

- (void)addImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    // 法一
    imageView.image = [UIImage imageNamed:@"PC.jpg"];
 
    
    NSString *oldImagePath = [[NSBundle mainBundle] pathForResource:@"PC" ofType:@"jpg"];
    // 根据路径获取图片
    
    // 法二 类方法 UIImage *image2
    //imageView.image = [UIImage imageWithContentsOfFile:oldImagePath];
    
    // 法三 对象方法 UIImage *image2
    //imageView.image = [[UIImage alloc] initWithContentsOfFile:oldImagePath];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview: imageView];
}

5.3 注意点

1、imageNamed:

第一次读取图片的时候,先把这个图片放到缓存中,下次再使用这个名字的图片时,直接从缓存中读取;如果缓存中不存在这个名字的图像,则先把该图片加载到缓存中,再返回该对象。如果我们需要短时间内频繁的加载一些一次性的图像,最好不要使用这种方法。

优点:方便快捷,只有第一次使用的时候稍慢,接下来再使用就会稍微快点;

缺点:如果在当前工程中只使用一次,就会浪费内存。

2、imageWithContentsOfFile:和initWithContentsOfFile:

每次都根据路径去读取图片,不经过系统缓存,直接从文件系统中加载并返回。不会耗内存,如果加载的图片在工程中只使用一次,应该选择这个方法。当收到内存警告的时候,系统可能会将UIImage内部的存储图像的内存释放,下一次需要的时候重新加载。

6、UITabBarController

6.1 介绍

选项卡控制器,与导航控制器一样,也被广泛用于各种ios应用程序。顾名思义,选项卡控制器在屏幕底部显示一系列“选显卡”,这些选项卡表示为图标和文本,用户触摸它们将在不同的场景间切换。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。与导航控制器一样,选项卡控制器会为我们处理一切。当用户触摸按钮时会在场景之间进行切换,我们无需以编程的方式处理选项卡栏事件,也无需手工在视图控制器之间切换。

6.2 语法

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {   
// 设置窗口的跟控制器
UITabBarController *tabbarVC = [[UITabBarController alloc] init];

// 添加子控制器
UIViewController *VC01 = [[UIViewController alloc] init];
// 设置标题
VC01.tabBarItem.title = @"精华";
// 设置默认图片
VC01.tabBarItem.image = [UIImage imageNamed:@"y.png"];
// 设置选中图片
VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"z.png"];
//VC01.view.backgroundColor = [UIColor yellowColor];
[tabbarVC addChildViewController:VC01];

UIViewController *VC02 = [[UIViewController alloc] init];
VC02.tabBarItem.title = @"新帖";
VC02.tabBarItem.image = [UIImage imageNamed:@"y.png"];
VC02.tabBarItem.selectedImage = [UIImage imageNamed:@"z.png"];
//VC02.view.backgroundColor = [UIColor redColor];
[tabbarVC addChildViewController:VC02];

UIViewController *VC03 = [[UIViewController alloc] init];
VC03.tabBarItem.title = @"关注";
VC03.tabBarItem.image = [UIImage imageNamed:@"y.png"];
VC03.tabBarItem.selectedImage = [UIImage imageNamed:@"z.png"];

//VC03.view.backgroundColor = [UIColor blueColor];
[tabbarVC addChildViewController:VC03];

UIViewController *VC04 = [[UIViewController alloc] init];
VC04.tabBarItem.title = @"我";

VC04.tabBarItem.image = [UIImage imageNamed:@"y.png"];
VC04.tabBarItem.selectedImage = [UIImage imageNamed:@"z.png"];

//VC04.view.backgroundColor = [UIColor greenColor];
[tabbarVC addChildViewController:VC04];

self.window.rootViewController = tabbarVC;

// 显示窗口
[self.window makeKeyAndVisible];
}

7、UINavigationController

7.1 介绍

UINavigationController用来管理视图控制器,在多视图控制器中常用。它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹入弹出控制器,最多只能显示一个视图控制器,那就是处于栈顶的视图控制器。

7.2 语法

1、创建导航控制器

UINavigationController *navigation = [[UINavigationController alloc] init];

2、将需要NavigationController 管理的视图添加进NavigationController 的子视图数组中

UINavigationController *navigation = [[UINavigationController alloc] init];
navigation.viewControllers = @[firstViewController,secondViewController,thirdViewController];

3、添加子控制器后,将navigation设置为窗口的根控制器

self.window.rootViewController = navigation;

先添加的视图被放在了栈底,显示在我们面前的是栈顶视图。点击back会把栈顶视图弹出,即为出栈(pop)操作。

4、在导航栏添加按钮

UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
    
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    
self.navigationItem.rightBarButtonItems = @[button1, button2];

5、修改右按钮格式

self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
//左按钮同理

6、隐藏back按钮

[self.navigationItem setHidesBackButton:YES];

7、界面跳转

从FirstViewController跳转到SceondViewController

在FirstViewController.m中,添加button,并在button事件中添加如下代码:

 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 [self.navigationController pushViewController:secondViewController animated:YES];

从SceondViewController返回FirstViewController:

[self.navigationController popViewControllerAnimated:YES];

注意:从后一个界面跳转回前一个界面,一定要用popViewController,千万不要alloc一个FirstViewController,push到FirstViewController

[self.navigationController popToRootViewControllerAnimated:YES];//这个方法可以跳转回第一个界面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值