iOS开发基础

第一课

UI-UIView父子类视图自动缩放

#import "XGOAppDelegate.h"

@implementation XGOAppDelegate

{

    UIView *_backView;

}

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

{

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    UIView *view1 = [[UIView alloc] init];

    view1.frame = CGRectMake(10, 30, 300, 30);

    view1.backgroundColor = [UIColor redColor];

    [self.window addSubview:view1];

    UIView *view2 = [[UIView alloc] init];

    view2.frame = CGRectMake(5, 5, 100, 20);

    view2.backgroundColor = [UIColor yellowColor];

    [view1 addSubview:view2];

    UIView *view3 = [[UIView alloc] init];

    view3.frame = CGRectMake(100, 5, 100, 20);

    view3.backgroundColor = [UIColor blueColor];

    [view1 addSubview:view3];

  //得到父类视图,在这里view2、view3相当于子类视图,view1相当于父类视图

    UIView *superView = view2.superview;

  //将父类视图(view1)背景颜色设置为黑色

    superView.backgroundColor = [UIColor blackColor];

  //得到子类视图数组

    NSArray *subViews = view1.subviews;

    NSLog(@"count = %d",subViews.count);

    //view2,view3变红了

//    for (UIView *view in subViews) {

//        view.backgroundColor = [UIColor redColor];

//    }

    

    UIView *view = [subViews objectAtIndex:0];

    view.backgroundColor = [UIColor redColor];

    UIView *blueview = [[UIView alloc] init];

    blueview.frame = CGRectMake(10, 100, 300, 30);

    blueview.backgroundColor = [UIColorblueColor];

  //如果子视图高度超出父视图,可用clipsToBounds属性:YES处理

    blueview.clipsToBounds = YES;

    [self.window addSubview:blueview];

    UIView *greenView = [[UIView alloc] init];

    greenView.frame = CGRectMake(10, 5, 200, 100);

    greenView.backgroundColor = [UIColor greenColor];

  //设置透明度,数越小,透明度越高

    greenView.alpha = 0.5;

    [blueview addSubview:greenView];

    //自动布局

    _backView = [[UIView alloc] init];

    _backView.frame = CGRectMake(100, 300, 120, 120);

    _backView.backgroundColor = [UIColor blackColor];

  //准许子类视图自动布局

    _backView.autoresizesSubviews = YES;

    [self.windowaddSubview:_backView];

    UIView *topView = [[UIView alloc] init];

    topView.frame = CGRectMake(10, 10, 100, 100);

    topView.backgroundColor = [UIColor orangeColor];

    //设置自动布局方式宽度随着父类自动增加

//    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    //如果宽度与高度同时增加用或 | 表示

    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [_backView addSubview:topView];

    [NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(timeTick) userInfo:nilrepeats:YES];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.windowmakeKeyAndVisible];

    returnYES;

}

-(void)timeTick

{

    _backView.frame = CGRectMake(_backView.frame.origin.x, _backView.frame.origin.y, _backView.frame.size.width+5, _backView.frame.size.height+5);

}

@end

原文地址:https://www.cnblogs.com/wuhao0918/p/4522787.html