iOS9 UIStackView很好用的控件,无需任何约束

先上效果图

新建项目打开storyboard,ViewController里拖入这些控件

上面的stackView设置

下面的stackView设置

 

为上面的stackView 添加约束

为下面的stackView 添加约束

下面添加代码

//
//  ViewController.m
//  20171102
//
//  Created by ZSY on 17/11/2.
//  Copyright © 2017年 ZSY. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIStackView *stackView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@property (nonatomic,assign) BOOL isLarge;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    for (NSInteger i = 0; i < 3 ; i ++) {
        UIImageView *imageView = [self valueForKey:[NSString stringWithFormat:@"imageView%zd",i + 1]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
    }
}
- (void)clickImageView:(UITapGestureRecognizer *)tap {
    long long tag = tap.view.tag;
    self.isLarge = !self.isLarge;
    switch (tag) {
        case 1000:
            if (!self.isLarge) {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView2.hidden = NO;
                    self.imageView3.hidden = NO;
                }];
            } else {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView2.hidden = YES;
                    self.imageView3.hidden = YES;
                }];
            }
            break;
        case 1001:
            if (!self.isLarge) {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView1.hidden = NO;
                    self.imageView3.hidden = NO;
                }];
            } else {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView1.hidden = YES;
                    self.imageView3.hidden = YES;
                }];
            }
            break;
        case 1002:
            if (!self.isLarge) {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView2.hidden = NO;
                    self.imageView1.hidden = NO;
                }];
            } else {
                [UIView animateWithDuration:0.5 animations:^{
                    self.imageView2.hidden = YES;
                    self.imageView1.hidden = YES;
                }];
            }
            break;
        default:
            break;
    }
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)deviceOrientationDidChange {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    NSLog(@"deviceOrientationDidChange:%ld",(long)orientation);
    if (orientation == UIDeviceOrientationPortrait) {
        self.stackView.axis = UILayoutConstraintAxisVertical;
    } else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
        self.stackView.axis = UILayoutConstraintAxisHorizontal;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

 

 

 

 

转载于:https://my.oschina.net/zsyzone/blog/1560277

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值