iOS开发UI基础―手写控件,frame,center和bounds属性

| 收藏本文 下载本文 作者:Xuan哒哒哒

以下是小编精心整理的iOS开发UI基础―手写控件,frame,center和bounds属性(共含2篇),仅供参考,希望能够帮助到大家。同时,但愿您也能像本文投稿人“Xuan哒哒哒”一样,积极向本站投稿分享好文章。

iOS开发UI基础―手写控件,frame,center和bounds属性

篇1:iOS开发UI基础―手写控件,frame,center和bounds属性

一、手写控件

1.手写控件的步骤

(1)使用相应的空间类创建控件对象

(2)设置该控件的各种属性

(3)添加控件到视图中

(4)如果是button等控件,还需考虑控件的单击事件等

(5)注意:View Contollor和view的关系

2.注意点

在OC开发中,Storyboard中的所有操作都可以通过代码实现,程序员一定要熟练掌握代码布局界面的能力!

设置控件监听方法的示例代码如下:

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

提示:

1> addTarget方法定义在UIControl类中,这意味着可以给所有继承自UIControl类的对象添加监听方法

2> 监听方法的第一个参数就是对象本身

3> 监听方法的第二个参数是监听控件的事件

3.代码示例

复制代码

1  //1.使用类创建一个按钮对象

2  // UIButton *headbtn=[[UIButton alloc] initWithFrame.:CGRectMake(100 ,100, 100, 100)];

3  //设置按钮对象为自定义型

4  UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom];

5

6  //2.设置对象的各项属性

7  //(1)位置等通用属性设置

8  headbtn.frame=CGRectMake(100, 100, 100, 100);

9

10  //(2)设置普通状态下按钮的属性

11  [headbtn setBackgroundImage:[UIImage imageNamed:@“i”] forState:UIControlStateNormal];

12  [headbtn setTitle:@“点我!” forState:UIControlStateNormal];

13  [headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

14

15  //(3)设置高亮状态下按钮的属性

16  [headbtn setBackgroundImage:[UIImage imageNamed:@“a”] forState:UIControlStateHighlighted];

17  [headbtn setTitle:@“还行吧~” forState:UIControlStateHighlighted];

18  [headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

19

20  //3.把对象添加到视图中展现出来

21  [self.view addSubview:headbtn];

22  //注意点!

23  self.headImageView=headbtn;

复制代码

二、frame,center和bounds属性

1.frame、center和bounds属性

frame.:控制位置和大小

center:控制位置(中心点)

bounds:控制大小(以自己的左上角为原点)

2.注意点

(1)通过以下属性可以修改控件的位置

frame.origin

center

(2)通过以下属性可以修改控件的尺寸

frame.size

bounds.size

3.代码示例

一个控制图片上下左右平移,缩放的程序(frame、center和bounds属性)

复制代码

1 //

2 // YYViewController.m

3 // 01-练习使用按钮的frame和center属性

4 //

5 // Created by apple on 14-5-21.

6 // Copyright (c) itcase. All rights reserved.

7 //

8

9 #import “YYViewController.h”

10

11 //私有扩展

12 @interface YYViewController

13

14 @property(nonatomic,weak)IBOutlet UIButton *headImageView;

15 @end

16

17 @implementation YYViewController

18

19 //枚举类型,从1开始

20 typedef enum

21 {

22  ktopbtntag=1,

23  kdownbtntag,

24  krightbtntag,

25  kleftbtntag

26 }btntag;

27

28 //viewDidLoad是视图加载完成后调用的方法,通常在此方法中执行视图控制器的初始化工作

29 - (void)viewDidLoad

30 {

31

32  //在viewDidLoad方法中,不要忘记调用父类的方法实现

33  [super viewDidLoad];

34

35

36  //手写控件代码

37  //一、写一个按钮控件,上面有一张图片

38

39  //1.使用类创建一个按钮对象

40  // UIButton *headbtn=[[UIButton alloc] initWithFrame.:CGRectMake(100 ,100, 100, 100)];

41  //设置按钮对象为自定义型

42  UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom];

43

44  //2.设置对象的各项属性

45  //(1)位置等通用属性设置

46  headbtn.frame=CGRectMake(100, 100, 100, 100);

47

48  //(2)设置普通状态下按钮的属性

篇2:iOS开发UI篇―UITableview控件基本使用

一、一个简单的英雄展示程序

NJHero.h文件代码(字典转模型)

复制代码

1 #import

2

3 @interface NJHero : NSObject

4 /**

5 * 头像

6 */

7 @property (nonatomic, copy) NSString *icon;

8 /**

9 * 名称

10 */

11 @property (nonatomic, copy) NSString *name;

12 /**

13 * 描述

14 */

15 @property (nonatomic, copy) NSString *intro;

16

17 - (instancetype)initWithDict:(NSDictionary *)dict;

18 + (instancetype)heroWithDict:(NSDictionary *)dict;

19 @end

复制代码

NJViewController.m文件代码

复制代码

1 #import “NJViewController.h”

2 #import “NJHero.h”

3

4 @interface NJViewController ()

5 /**

6 * 保存所有的英雄数据

7 */

8 @property (nonatomic, strong) NSArray *heros;

9 @property (weak, nonatomic) IBOutlet UITableView *tableView;

10

11 @end

12

13 @implementation NJViewController

14

15 #pragma mark - 懒加载

16 - (NSArray *)heros

17 {

18  if (_heros == nil) {

19    // 1.获得全路径

20    NSString *fullPath = [[NSBundle mainBundle] pathForResource:@“heros” ofType:@“plist”];

21    // 2.更具全路径加载数据

22    NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];

23    // 3.字典转模型

24    NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];

25    for (NSDictionary *dict in dictArray) {

26      NJHero *hero = [NJHero heroWithDict:dict];

27      [models addObject:hero];

28    }

29    // 4.赋值数据

30    _heros = [models copy];

31  }

32  // 4.返回数据

33  return _heros;

34 }

35

36 - (void)viewDidLoad

37 {

38  [super viewDidLoad];

39  // 设置Cell的高度

40  // 当每一行的cell高度一致的时候使用属性设置cell的高度

41  self.tableView.rowHeight = 60;

42  self.tableView.delegate = self;

43 }

44

45 #pragma mark - UITableViewDataSource

46 // 返回多少组

47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

48 {

49  return 1;

50 }

51 // 返回每一组有多少行

52 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

53 {

54  return self.heros.count;

55 }

56 // 返回哪一组的哪一行显示什么内容

57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

58 {

59  // 1.创建CELL

60  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle.:UITableViewCellStyleSubtitle reuseIdentifier:nil];

61  // 2.设置数据

62  // 2.1取出对应行的模型

63  NJHero *hero = self.heros[indexPath.row];

64  // 2.2赋值对应的数据

65  cell.textLabel.text = hero.name;

66  cell.detailTextLabel.text = hero.intro;

67  cell.imageView.image = [UIImage imageNamed:hero.icon];

68  // 3.返回cell

69  return cell;

70 }

71 #pragma mark - UITableViewDelegate

72 /*

73 // 当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度

74 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

75 {

76  if (1 == indexPath.row) {

77    return 180;

78  }

79  return 44;

80 }

81 */

82

83 #pragma mark - 控制状态栏是否显示

84 /**

85 * 返回YES代表隐藏状态栏, NO相反

86 */

87 - (BOOL)prefersStatusBarHidden

88 {

89  return YES;

90 }

91 @end

复制代码

实现效果:

代码注意点:

(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能

NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];

iOS开发UI基础―手写控件,frame,center和bounds属性(锦集2篇)

欢迎下载DOC格式的iOS开发UI基础―手写控件,frame,center和bounds属性,但愿能给您带来参考作用!
推荐度: 推荐 推荐 推荐 推荐 推荐
点击下载文档 文档为doc格式

相关文章

热门推荐

HOT

猜你喜欢

NEW
点击下载本文文档