WHC_AutoLayoutExample: 致力打造使用最簡單功能最強大的自動布局開源庫

jopen 8年前發布 | 54K 次閱讀 Objective-C開發 WHC_AutoLayoutExample

WHC_AutoLayoutKit

聯系QQ: 712641411

開發作者: 吳海超

iOS技術交流群: 302157745

WHC_AutoLayoutKit 致力打造使用最簡單功能最強大的自動布局開源庫

WHC_AutoLayoutKit 借鑒android的相對布局和線性布局

一行代碼約束四個方向使用例子

/// 自動拉伸寬度與父視圖右邊間隙為10px, 高度為30px
[_whcView whc_FrameAutoWidth:WHCWidthAutoRectMake(10, 0, 10, 30)];

/// 自動拉伸高度與父視圖底部間隙為10px 寬度為100px [_whcView whc_FrameAutoHeight:WHCHeightAutoRectMake(10, 0, 100, 10)];

/// 自動拉伸寬度和高度與父視圖右邊底部間隙分別為10px [_whcView whc_FrameAuto:WHCAutoRectMake(10, 0, 10, 10)];

/// 普通對視圖添加約束方式 [view1 whc_LeftSpace:10]; [view1 whc_TopSpace:74]; [view1 whc_WidthEqualView:view2]; [view1 whc_Height:150];

[view2 whc_LeftSpace:10 relativeView:view1]; [view2 whc_TopSpace:74]; [view2 whc_RightSpace:10]; [view2 whc_HeightEqualView:view1];</pre> </div>

一行代碼計算UITableViewCell高度例子

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /// 僅需調用該方法即可自動計算cell高度
    return [DemoVC2Cell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

StockView容器視圖自動橫向布局例子(可設置子視圖寬度拉伸權重比)

// 創建容器視圖
container = [WHC_LayoutContainer new];
// 對容器視圖1進行約束布局
[container whc_FrameAutoWidth:WHCWidthAutoRectMake(0, 64, 0, 150)];
// 對容器視圖1配置
// 容器子視圖上下左右間隙
container.whc_Edge = UIEdgeInsetsMake(10, 10, 10, 10);
// 自動橫向布局設置
container.whc_Orientation = Horizontal;
// 容器子視圖之間間隙
container.whc_Space = 10; 
// 開始對容器進行自動布局

// 添加子元素視圖 [container addSubview:lable1]; [container addSubview:lable2]; [container addSubview:lable3]; [container addSubview:lable4];

// 設置容器子視圖寬度拉伸權重 label1.whc_WidthWeight = 2

// 容器開始布局 [container whc_StartLayout];</pre> </div>

StockView容器視圖自動垂直布局例子(可設置子視圖高度拉伸權重比) 與橫向自動布局容器一樣只是方向參數為Vertical

StockView容器視圖自動橫向垂直混合布局例子(可設置子視圖寬高拉伸權重比)

// 創建容器視圖
container = [WHC_LayoutContainer new];
// 對容器視圖1進行約束布局
[container whc_FrameAutoWidth:WHCWidthAutoRectMake(0, 64, 0, 150)];
// 對容器視圖1配置
// 容器子視圖上下左右間隙
container.whc_Edge = UIEdgeInsetsMake(10, 10, 10, 10);
// 自動橫向垂直混合布局設置
container.whc_Orientation = All;
// 容器子視圖之間間隙
container.whc_Space = 10; 
// 開始對容器進行自動布局

// 添加子元素視圖 [container addSubview:lable1]; [container addSubview:lable2]; [container addSubview:lable3]; [container addSubview:lable4]; [container addSubview:lable5];

// 設置容器子視圖寬度拉伸權重 label1.whc_WidthWeight = 2; label2.whc_HeightWeight = 2;

// 容器開始布局 [container whc_StartLayout];</pre> </div>

項目地址: https://github.com/netyouli/WHC_AutoLayoutExample

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!