您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iPhone 開發基礎:UIView 常用屬性與方法

編輯:iOS 開發綜合

常用屬性

  • alpha:視圖透明度,範圍 0.0f ~ 1.0f。
  • backgroundColor:背景顏色。
  • subviews:子視圖集合。
  • hidden:是否隱藏。
  • tag:視圖的唯一識別碼,整數值。
  • superview:父視圖。
  • multipleTouchEnabled:是否啟用多點觸控。
  • userInteractionEnabled:是否響應觸摸事件。

常用方法

- (void)removeFromSuperview;
// 從父視圖中移除當前視圖

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
// 插入到指定的索引位置

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
// 交換兩個索引位置上的視圖

- (void)addSubview:(UIView *)view;
// 在當前視圖層之上加入新視圖

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
// 插入到指定視圖的下方

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
// 插入到指定視圖的上方

- (void)bringSubviewToFront:(UIView *)view;
// 把指定視圖移到最前面

- (void)sendSubviewToBack:(UIView *)view;
// 把指定視圖移到最后面

- (UIView *)viewWithTag:(NSInteger)tag;
// 依 tag 取得已存在的視圖物件

純手寫方式建立視圖階層

有兩種做法:一是完全用程式碼建立,不使用 Storyboard 與 ViewController 來管理;二是使用 Xcode 提供的 UIView 與 UIWindow。以下示範前者。

步驟一:移除樣板檔案

刪除專案中的 ViewController.h、ViewController.m、Main.storyboard 與 LaunchScreen.xib。

步驟二:清掉相關設定

把 Target 的 Main Interface 設為空,Launch Screen File 也一并清空。

步驟三:在 AppDelegate 中建立視窗與視圖

#import "AppDelegate.h"

@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];

    // 子視圖:綠色,tag = 100
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 160)];
    greenView.tag = 100;
    greenView.backgroundColor = [UIColor greenColor];
    [bgView addSubview:greenView];

    // 子視圖:藍色,tag = 101
    // 透過 superview 可以反向改到父視圖的屬性
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(20, 190, 280, 160)];
    blueView.tag = 101;
    blueView.backgroundColor = [UIColor blueColor];
    blueView.superview.backgroundColor = [UIColor lightGrayColor];
    [bgView addSubview:blueView];

    // 按鈕
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 240, 40)];
    [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];

    // 用 tag 取回視圖並修改
    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

執行結果

結果分析

這裡的視圖是有層次的:最底層是作為父視圖的 bgView,其餘都是它的子視圖,依程式碼中設定的座標與尺寸堆疊其上。

幾個值得注意的地方:

  • 子視圖可以透過 superview 反向取得父視圖並修改其屬性,這是很多人忽略的用法。
  • 後加入的視圖預設在上層。範例中先加入綠色再加入藍色,因此藍色會蓋住重疊部分;若要調整層序,用 bringSubviewToFront: 或 insertSubview:aboveSubview:。
  • 設定 userInteractionEnabled = NO 後,該視圖及其子視圖都不再接收觸摸事件,事件會穿透到下方視圖。
  • tag 在同一父視圖下必須唯一,否則 viewWithTag: 只會回傳最上層的那一個。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved