IOS UIView

uiview 的方法

  //定義3個view

   UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    view1.backgroundColor = [UIColor redColor];

    [self.window addSubview:view1];

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 150, 100, 100)];

    view2.backgroundColor = [UIColor yellowColor];

    [self.window addSubview:view2];

    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(140, 200, 100, 100)];

    view3.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view3];

  

    //UIView添加視圖的方法

    //1.在指定的Index處插入子視圖

    [self.window insertSubview:view2 atIndex:0];

    //2.在指定的視圖上面添加子視圖

    [self.window insertSubview:view1 aboveSubview:view2];

    //3.在指定的視圖下⾯面添加⼦子視圖

    [self.window insertSubview:view3 belowSubview:view2];

    

    //UIview 管理視圖層次

    //1.把指定的視圖移動到最前面

    [self.window bringSubviewToFront:view1];

    //2.把指定的子視圖移動到最後面

    [self.window sendSubviewToBack:view2];

    //3.交換兩個指定索引位置子視圖

    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

    //4.刪除某個子視圖

    [view3 removeFromSuperview];

    //UIView 視圖屬性

    //1.hidden控制視圖的顯隱

    view1.hidden = YES;

    //2.alpha控制視圖的透明度(0-1)

    view1.alpha = 0.5;

    //3.superview 獲取本視圖的父視圖

    UIView * superView = [view1 superview];

    //調用父視圖改變屬性

    superView.backgroundColor = [UIColor yellowColor];

    //4.subviews 獲取本視圖的所有子視圖  他取出來是以數組形式存放

    NSArray * subview = [self.window subviews];

    //調用子視圖改變屬性

    ((UIView*)subview[0]).backgroundColor = [UIColor grayColor];

    //5.tag 給視圖添加標記,被加完標記的視圖可以使用viewWithTag:方法取出

    view1.tag = 101;

    UIView *view = [self.window viewWithTag:101];

    view.alpha = 0.1;


發佈了28 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章