iPone应用开发 UIView 常用属性和方法

程序开发-uiview常用属性和方法">iPone应用程序开发 UIView常用属性和方法

常用属性

alpha 视图的透明度0.0f - 1.0f backgroundColor 视图的背景颜色 subviews 子视图的集合 hidden 视图是否隐藏 tag 视图的唯一标示符,是一个整形数据 superview 视图的父视图 multipleTouchEnabled 是否开启多点触控 userInteractionEnable 是否响应触摸事件

常用方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code class="hljs" objectivec="">- (void)removeFromSuperview;

//从父视图中删除当前视图

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

//从指定的索引位置插入视图

 

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

//根据制定视图的索引位置交换视图

- (void)addSubview:(UIView *)view;

//在当前视图层的上面添加一个新视图,这个方法会对参数视图的引用加1

 

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

//在指定视图下方添加一个视图

- (void)insertSubview:(UIView *)view aboveSubVIew:(UIView *)siblingSubview;

//在指定的上方添加一个视图

 

- (void)bringSubviewToFront:(UIView *)view;

//将参数指定的 view 移动到视图的最前面

- (void)sendSubviewToBack:(UIView * )view;

//将参数指定的 view 移动到视图的最后面

 

- (UIView *)viewWithTag:(NSInteger)tag;

//根据tag 属性获取已存在的 UIView 对象

</code>

具体实现

?

1

2

3

4

<code>实现的方法:   

     1.可以纯代码实现,主要使用手动写代码的方式实现,不使用Xcode中的自动加载主视图的方法,我们手动管理视图,不用 ViewController 来管理.

     2. 可以使用Xcode提供的UIView和UIVindow来实现   

</code>

实现方法

第一步:
首先,删掉工程中的 ViewController.h,ViewController.m,Main.storyboard 和 LaunchScre.xil 文件,我们自己手动写加载窗口和视图。
第二步:
  把工程的 Main interface 中的 Main 删除,并把 Launch Screen File 文件中的LaunchScreen 也删掉.
接下来,我们在 AppDelegate.m 文件中写代码,加载主视图和窗口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

<code class="hljs" objectivec="">   

#import AppDelegate.h

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor grayColor];

 

    UIView *bgView = [[UIView alloc] initWithFrame:self.window.bounds];

    bgView.backgroundColor = [UIColor orangeColor];

    [self.window addSubview:bgView];

 

    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(2020280160)];

    greenView.tag = 100;//这个 tag 是视图的唯一标示,实际只是一个整数值

    greenView.backgroundColor = [UIColor greenColor];

 

    //bgView 和 greenView 建立了一种关系,即父子关系  bgView 叫做父视图, greenView 叫子视图

//    [bgView addSubview:greenView];

    greenView.superview.backgroundColor = [UIColor redColor];

 

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(20190,280160)];

    blueView.tag = 101;

    blueView.backgroundColor = [UIColor blueColor];

//    [bgView addSubview:blueView];

 

//    //通过子视图改变父视图的背景颜色

    blueView.superview.backgroundColor = [UIColor lightGrayColor];

 

    [bgView addSubview:blueView];

    [bgView addSubview:greenView];

 

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(202024040)];

    [btn setTitle:@别点我 forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

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

    btn.backgroundColor = [UIColor lightGrayColor];

 

    [bgView addSubview:btn];

 

//    greenView.userInteractionEnabled = NO;

//    [greenView addSubview:btn];

    UIView *gView = [bgView viewWithTag:100];

    gView.backgroundColor = [UIColor purpleColor];

 

    UIView *bView = [bgView viewWithTag:101];

    bView.backgroundColor = [UIColor redColor];

 

    [self.window makeKeyAndVisible];

    return YES;

}

 

- (void)onButton

{

    NSLog(@%s,__func__);

}

 

 

@end

</code>

运行结果如下:

结果分析:
这里的视图是有层次的,最底层的是橙色的视图 bgView,就是父视图,其他的都是子视图,其它视图按照代码中的位置和大小放在 bgView 上.父视图 baVIew 可以通过子视图来改变其属性.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值