国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發之手動布局子視圖

2020-02-19 15:46:17
字體:
來源:轉載
供稿:網友

iOS開發之手動布局子視圖,大家了解嗎?其實視圖布局采用是的磚石結構,但是圖片的文字都是不固定的,下文是武林技術頻道小編為大家帶來的介紹,希望能幫到您。

手動布局子視圖;
下面先看下效果圖,我們今天要實現的效果:

這里我們默認用storyboard啟動:
首先我們要在白色的屏幕上面創建一個父視圖SuperView(藍色的背景),在父視圖里面創建四個小視圖(橘黃色的背景)
下面看代碼,
在SuperView.h文件里面:

#import <UIKit/UIKit.h>@interface SuperView : UIView{ UIView * _view01; UIView * _view02; UIView * _view03; UIView * _view04;}//聲明創建視圖函數-(void) createSubViews;@end在SuperView.m文件里面:#import "SuperView.h"@interface SuperView ()@end@implementation SuperView-(void) createSubViews{ //左上角視圖 _view01 = [[UIView alloc] init]; _view01.frame=CGRectMake(0, 0, 40, 40); //右上角視圖 _view02 = [[UIView alloc] init]; _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40); //右下角視圖 _view03 = [[UIView alloc] init]; _view03.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40); //左下角視圖 _view04 = [[UIView alloc] init]; _view04.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40); _view01.backgroundColor=[UIColor orangeColor]; _view02.backgroundColor=[UIColor orangeColor]; _view03.backgroundColor=[UIColor orangeColor]; _view04.backgroundColor=[UIColor orangeColor]; [self addSubview:_view01]; [self addSubview:_view02]; [self addSubview:_view03]; [self addSubview:_view04];}//當需要重新布局時調用此函數//通過此函數重新設定子視圖的位置//手動調整子視圖的位置-(void)layoutSubviews{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; _view01.frame=CGRectMake(0, 0, 40, 40); _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40); _view03.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40); _view04.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40); [UIView commitAnimations];}@end

在ViewController.m文件里面:

#import "ViewController.h"#import "SuperView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //創建一個父視圖 SuperView * sView = [[SuperView alloc]init]; sView.frame = CGRectMake(20, 20, 180, 280); //父視圖調用函數創建四個小視圖 [sView createSubViews]; sView.backgroundColor = [UIColor blueColor]; [self.view addSubview:sView]; UIButton * btn01 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn01.frame = CGRectMake(240, 480, 80, 40); [btn01 setTitle:@"放大" forState:UIControlStateNormal]; [btn01 addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn01]; UIButton * btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn02.frame = CGRectMake(240, 520, 80, 40); [btn02 setTitle:@"縮小" forState:UIControlStateNormal]; [btn02 addTarget:self action:@selector(pressSmall) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn02]; sView.tag = 101;}//放大父視圖-(void) pressLarge{ SuperView * sView = (SuperView*)[self.view viewWithTag:101]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; sView.frame=CGRectMake(20, 20, 280, 400); [UIView commitAnimations];}//縮小父視圖-(void) pressSmall{ SuperView * sView = (SuperView*)[self.view viewWithTag:101]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; sView.frame=CGRectMake(20, 20, 180, 280); [UIView commitAnimations];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

以上就是iOS開發之手動布局子視圖的全部介紹,其實學習的方法有多種多樣,小伙伴們可以通過js.Vevb.com來了解更加詳細的知識哦。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拉萨市| 井陉县| 申扎县| 永济市| 花垣县| 洮南市| 德江县| 泌阳县| 扎鲁特旗| 永安市| 济阳县| 耒阳市| 钦州市| 辽阳县| 枣阳市| 蓝山县| 嘉峪关市| 临城县| 乡城县| 咸丰县| 永寿县| 高州市| 临泉县| 分宜县| 交口县| 元谋县| 应城市| 徐汇区| 区。| 黄骅市| 松滋市| 两当县| 峡江县| 崇州市| 宿州市| 闸北区| 珠海市| 玛纳斯县| 喜德县| 扎囊县| 成武县|