iOS 7 使用CGAffineTransformMakeRotation在autolayout布局下旋转图片变形.

来源:互联网 发布:淘宝卖家常用app 编辑:程序博客网 时间:2024/06/06 02:31

今天将项目的布局换成了autolayout,结果遇到一个很奇葩的问题,使用CGAffineTransformMakeRotation旋转图片时,在iOS 8以上的版本都没有问题,但是在iOS 7上出现了怪怪的效果.想都不用想肯定是autolayout的问题喽,简单粗暴的方法,换回手码呗.可是对于一个懒货来说,能拖线解决的绝对不手码.因为不同于之前了嘛,现在4种屏幕了,手码好累委屈


错误原因: 约束设置的是水平和垂直位置固定,距离左右边距固定,等比例拉伸.约束如下:


因为图片是正方形的,设置距离边距固定值的话,转到角角处图片就给压缩了.(手残,大家凑合看吧) 有人会问,为什么在iOS 8以上就没问题呀?只能告诉你苹果在高版本上处理了这个比较不符合常理的问题吧偷笑



解决办法:

1. 上面说的,简单粗暴的方法:关掉autolayout,使用纯手码

2. 你可以偷点懒,因为高版本的没有问题,只需要适配一下低版本就好了.思路是不设置边距就不会拉伸了,所以设置水平和垂直距离然后固定死大小就好了.当然你不能在storyboard中同时设置这两种,所以只能手码了,不过肯定比你纯手码简单.

    //箭头适配

    if ([UIDevicecurrentDevice].systemVersion.floatValue <8.0) {

       NSMutableArray* cons = [NSMutableArrayarray];

       for (NSLayoutConstraint* conin self.view.constraints)

           if (con.firstItem ==self.arrow || con.secondItem ==self.arrow)

                [consaddObject:con];

        

        [self.viewremoveConstraints:cons];

        [self.arrowremoveConstraints:self.arrow.constraints];

        [self.viewaddConstraint:

         [NSLayoutConstraintconstraintWithItem:self.arrowattribute:NSLayoutAttributeCenterXrelatedBy:0toItem:self.viewattribute:NSLayoutAttributeLeftmultiplier:1constant:self.arrow.center.x]];

        [self.viewaddConstraint:

         [NSLayoutConstraintconstraintWithItem:self.arrowattribute:NSLayoutAttributeCenterYrelatedBy:0toItem:self.viewattribute:NSLayoutAttributeTopmultiplier:1constant:self.arrow.center.y]];

        [self.arrowaddConstraint:

         [NSLayoutConstraintconstraintWithItem:self.arrowattribute:NSLayoutAttributeWidthrelatedBy:0toItem:nilattribute:0multiplier:1constant:self.arrow.bounds.size.width]];

        [self.arrowaddConstraint:

         [NSLayoutConstraintconstraintWithItem:self.arrowattribute:NSLayoutAttributeHeightrelatedBy:0toItem:nilattribute:0multiplier:1constant:self.arrow.bounds.size.height]];

    }


注意: 别人说设置锚点,其实没有用的.

_arrow.layer.anchorPoint =CGPointMake(0.5,0.5);

0 0
原创粉丝点击