ipad流水布局及其旋转界面view间隔布局调整

控制器嵌套关系如下,ITGDealViewControllerviewUI界面中Dock其中点击按钮弹出的一个子view

rootViewController == mianViewController --> UINavigationController -->ITGDealViewController -->UICollectionViewFlowLayout

*****************************************************************

ITGAppDelegate.m文件------------------------------中的设置根控制器

-----------------------------------------------------------------

//设置根控制器

self.window.rootViewController = [[ITGMainViewController alloc]init];

ITGMainViewController.m文件

-----------------------------------------------------------------

//创建ITGDealViewController

ITGDealViewController *vdeal = [[ITGDealViewController alloc]init];

[self addOneChildVc:deal];

-(void)addOneChildVc:(UIViewController *)vc

{

    //导航控制器UINavigationController中有其他四个子控制器

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

    //主控制器ITGMainViewController中有子控制器:导航控制器UINavigationController

    [self addChildViewController:nav];

    

    

ITGDealViewController.m文件

-----------------------------------------------------------------

    

//重写init方法

- (id)init

{

    //流水布局

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    //每个格子大小

    layout.itemSize = CGSizeMake(250, 250);

    //设置每一行之间的距离

    layout.minimumLineSpacing = 20;

    //调用父类的构造方法

    if ([super initWithCollectionViewLayout:layout]) {

    }

}

    

//ipad屏幕旋转监听方法:子控制器和根控制器有关联才能知道屏幕旋转了

    //旋转后调用的方法

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

    //返回的是父类UICollectionViewLayout,子类是UICollectionViewFlowLayout,把返回的父类要强转成我们要用上面的子类

    //取出布局

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;

    

    //设置collectionView内边距 布局

    CGFloat paddingY =20;

    CGFloat paddingX = 40;

    layout.sectionInset = UIEdgeInsetsMake(paddingY, paddingX, paddingY, paddingX);

    //判断横竖屏

    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))//在旋转后当前横屏

    {

        paddingX = (self.view.bounds.size.width - 3*layout.itemSize.width)/4;

    }else{

        paddingX = (self.view.bounds.size.width - 2*layout.itemSize.width)/3;

    }

    //添加动画

    [UIView animateWithDuration:0.5 animations:^{

        //行之间

        layout.minimumLineSpacing = paddingY;

    }];

}

//即将旋转调用的方法  本例没有有使用

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

    

}

    

//想一进界面就有这样的效果,就在-(void)viewDidLoad或者- (void)viewDidAppear:(BOOL)animated调用设置界面布局效果的方法,具体调用哪个视情况而定

[self didRotateFromInterfaceOrientation:0];

//如本例适合调用的方法

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    //调用设置界面布局效果的方法

    [self didRotateFromInterfaceOrientation:0];

}

    

    

原文地址:https://www.cnblogs.com/zuozeing/p/3563708.html