UIView的animateWithDuration表现不同的iOS 8 [英] UIView animateWithDuration behaves differently in iOS 8

查看:358
本文介绍了UIView的animateWithDuration表现不同的iOS 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚换的iOS从7.1到iOS 8.1。我遇到与的UIView animateWithDuration 下面的问题。图片是胜过千言万语,我每次的iOS下所作的行为视频:

iOS版7.1:

https://vimeo.com/113887812

的iOS 8.1:

https://vimeo.com/113887814

这是一个iPhone应用程序。

我要指出,我已经搜查和发布前等地。这似乎是一个公认的问题,但是我发现是使用基于约束的动画(我不明白),并关闭自动布局一些参考,但没有真正的共识。

我试过一对夫妇刺伤,但已经变得没有出路的。如果有人能指点(或走我走过)明确的修复,我真的AP preciate它。

我要指出,该应用程序的目的是只在纵向位置工作,所以我在开发过程中没有给思想自动布局。

参与以编程方式创建的所有意见,除了滚轮本身。

下面是code。我包括整个方法,这是pretty长,但我真的不明白在新的iOS已经改变了,所以我是在安全方面。在 animateWithDuration 调用pretty深成块:


   - (无效)displayDayChart
{
    [self.scroller setBackgroundColor:[的UIColor clearColor]];    * UIImage的图像= [UIImage的imageNamed:@GrayPaper.png];
    self.chartView.backgroundColor = [的UIColor colorWithPatternImage:图片];    [self.view addSubview:self.scroller];
    [self.scroller setScrollEnabled:YES];    [self.scroller setContentSize:CGSizeMake(320,1800)];
    [self.scroller setContentOffset:CGPointMake(0.0,0.0)动画:是];    dayBarArray = [[NSMutableArray里的alloc]初始化];
    infoLabelArray = [[NSMutableArray里的alloc]初始化];
    timeLabelArray = [[NSMutableArray里的alloc]初始化];    timeFrameDC = - 86400; // 24小时该试验中,在现实生活中可选择    [self.scroller.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];    timeframeDCStart = [NSDate的dateWithTimeIntervalSinceNow:timeFrameDC];
    timeframeDCEnd = [timeframeDCStart dateByAddingTimeInterval:ABS(timeFrameDC)];    行为predicate = NS predicate predicateWithFormat:@(的startTime> =%@和停止时间< =%@)或((的startTime< =%@和停止时间> =%@ )OR(的startTime< =%@和停止时间== NULL)),timeframeDCStart,timeframeDCEnd,timeframeDCStart,timeframeDCStart,timeframeDCEnd]    NSFetchedResultsController * dayActivityFRC = [TimedActivity MR_fetchAllSortedBy:@的startTime升:是用predicate:行为predicate GROUPBY:无代表:无];
    双taDuration;
    双activityPercent;
    INT barHeight;
    INT lastHeight = 0;
    INT tickerCount = 1;
    INT巴里;
    INT lastBarY = 0;
    的CGRect lastBarFrame;的#pragma标志DisplayDayChart设置    对(在dayActivityFRC.fetchedObjects TimedActivity *活动)
    {
        //创建酒吧和相关的标签        thisBar = [[DayChartBar页头] initWithFrame:方法CGRectZero];        thisInfoLabel = [[的UILabel页头] initWithFrame:方法CGRectZero];
        thisTimeLabel = [[的UILabel页头] initWithFrame:方法CGRectZero];
        arrowView = [[UIView的页头] initWithFrame:方法CGRectZero];        thisBar.tag = tickerCount;        thisInfoLabel.tag = thisBar.tag + 100;
        thisTimeLabel.tag = thisBar.tag + 200;        arrowView.tag = thisBar.tag + 300;
        * UIImage的图像= [UIImage的imageNamed:@LeftArrowRed.png];
        //将它们添加到滚动条        [self.scroller addSubview:thisBar];
        [self.scroller addSubview:thisInfoLabel];
        [self.scroller addSubview:thisTimeLabel];
        [self.scroller addSubview:arrowView];        NSCalendar *日历= [NSCalendar currentCalendar]
        如果(!activity.stopTime)//当前运行
        {
            如果([activity.startTime timeIntervalSinceDate:timeframeDCStart]℃的)
            {
                dayBarDurationString = @24:00:00;
                taDuration =(晶圆厂([timeframeDCStart timeIntervalSinceDate:timeframeDCEnd]));
            }            其他
            {
                NSDateComponents *组件= [日历组件:NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit寄件者:activity.startTime TODATE:timeframeDCEnd选项:0];
                NSInteger的时间= [组分小时]
                NSInteger的分钟= [组件分]
                NSInteger的秒= [第二组件]。
                dayBarDurationString = [的NSString stringWithFormat:@%02li:%02li:%02li(长)小时,(长)分,(长)秒]                taDuration =(晶圆厂([activity.startTime timeIntervalSinceDate:timeframeDCEnd]));
            }
        }        否则//无论是早期的边缘情况或完全封装
        {
            如果([activity.startTime timeIntervalSinceDate:timeframeDCStart]小于0)//早期边缘情况
            {
                NSDateComponents *组件= [日历组件:NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit寄件者:timeframeDCStart TODATE:activity.stopTime选项:0];
                NSInteger的时间= [组分小时]
                NSInteger的分钟= [组件分]
                NSInteger的秒= [第二组件]。
                dayBarDurationString = [的NSString stringWithFormat:@%02li:%02li:%02li(长)小时,(长)分,(长)秒]                taDuration =(晶圆厂([activity.stopTime timeIntervalSinceDate:timeframeDCStart]));
            }            否则如果([activity.startTime timeIntervalSinceDate:timeframeDCStart]&0)//封装
            {
                NSDateComponents *组件= [日历组件:NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit寄件者:activity.startTime TODATE:activity.stopTime选项:0];
                NSInteger的时间= [组分小时]
                NSInteger的分钟= [组件分]
                NSInteger的秒= [第二组件]。
                dayBarDurationString = [的NSString stringWithFormat:@%02li:%02li:%02li(长)小时,(长)分,(长)秒]                taDuration = [activity.duration的doubleValue];
            }        }
        NSDateFormatter * localDateFormatter = [[NSDateFormatter的alloc]初始化];
        [localDateFormatter setDateFormat:@MMM DD];        NSDateFormatter * localTimeFormatter = [[NSDateFormatter的alloc]初始化];
        [localTimeFormatter setDateFormat:@HH:MM一个];        * NSString的timeLabelDateString = [[的NSString的alloc]初始化];        * NSString的timeLabelTimeString = [[的NSString的alloc]初始化];        * NSString的timeLabelString = [[的NSString的alloc]初始化];
        如果((activity.startTime timeIntervalSinceDate:timeframeDCStart]℃,))
        {
            timeLabelDateString = [localDateFormatter stringFromDate:timeframeDCStart];
            timeLabelTimeString = [localTimeFormatter stringFromDate:timeframeDCStart];
        }        其他
        {
            timeLabelDateString = [localDateFormatter stringFromDate:activity.startTime];
            timeLabelTimeString = [localTimeFormatter stringFromDate:activity.startTime];
        }
        timeLabelString = [的NSString stringWithFormat:@%@ - %@,timeLabelDateString,timeLabelTimeString]        thisBar.endColor = activity.color;        activityPercent =晶圆厂((taDuration / timeFrameDC)* 100);        如果(activityPercent大于100)
        {
            activityPercent = 100;
        }        如果(activityPercent< 1.9)
        {
            activityPercent = 1.9;
        }
        barHeight = ABS((self.scroller.contentSize.height - 100)*(activityPercent / 100));        如果(tickerCount == 1)
        {
            巴里= self.scroller.contentSize.height -10;
        }        其他
        {
            巴里=(lastBarY - lastHeight);
        }的#pragma标志DisplayDayChart动画
        [UIView的animateWithDuration:0.8
                              延迟:0.0
                            选项​​:UIViewAnimationCurveEaseInOut //德precated,但仍然有效
                         动画:^
         {
             //开始状态*************************************             thisBar.frame = CGRectMake(20,self.scroller.contentSize.height,130,0);
             thisBar.backgroundColor =的UIColor blackColor]             thisInfoLabel.frame = CGRectMake(20,self.scroller.contentSize.height,thisBar.frame.size.width,30);             thisTimeLabel.frame = CGRectMake((thisBar.frame.origin.x)+(thisBar.frame.size.width + 35),self.scroller.contentSize.height,150,15);             thisTimeLabel.textColor =的UIColor blueColor]             arrowView.frame = CGRectMake(thisTimeLabel.frame.origin.x - 20,thisTimeLabel.frame.origin.y,16,16);
             thisInfoLabel.textColor =的UIColor clearColor]
             //结束状态*************************************             thisBar.frame = CGRectMake(20,巴里,130 - barHeight);
             thisBar.backgroundColor = thisBar.endColor;             thisBar.layer.shadowColor = [[的UIColor blackColor] CGColor];
             thisBar.layer.frame = CGRectInset(thisBar.layer.frame,0.0,3.0);             thisBar.layer.shadowOpacity = 0.7;
             thisBar.layer.shadowRadius = 4.0;
             thisBar.layer.shadowOffset = CGSizeMake(5.0F,5.0F);
             //位置infoLabel             thisInfoLabel.frame = CGRectMake(20,self.scroller.contentSize.height +((thisBar.frame.size.height / 2) - 30),thisBar.frame.size.width,30);
             thisTimeLabel.frame = CGRectMake((thisBar.frame.origin.x)+(thisBar.frame.size.width + 35),(thisBar.frame.origin.y)+(thisBar.frame.size.height) - 5, 150,15);             thisInfoLabel.textColor =的UIColor blackColor]             dayBarNameString = activity.name;             [thisInfoLabel并入setfont:[UIFont fontWithName:@值得一提的粗体尺寸:16.0]];             thisInfoLabel.numberOfLines = 0;             thisInfoLabel.textAlignment = NSTextAlignmentCenter;
// thisInfoLabel.layer.borderWidth = 1;
             thisInfoLabel.text = [的NSString stringWithFormat:@%@ \\ n%@,dayBarNameString,dayBarDurationString]             [thisInfoLabel sizeToFit]
             如果(thisInfoLabel.frame.size.height> = thisBar.frame.size.height)
             {
                 thisInfoLabel.text = [的NSString stringWithFormat:@%@,dayBarNameString]
             }             其他
             {
                 thisInfoLabel.text = [的NSString stringWithFormat:@%@ \\ n%@,dayBarNameString,dayBarDurationString]
             }             [thisInfoLabel sizeToFit]
             [thisInfoLabel setHidden:NO];             thisInfoLabel.center = thisBar.center;             arrowView.frame = CGRectMake(thisTimeLabel.frame.origin.x - 20,thisTimeLabel.frame.origin.y,16,16);
             arrowView.backgroundColor = [的UIColor colorWithPatternImage:图片];             thisTimeLabel.textColor =的UIColor blueColor]
             [thisTimeLabel并入setfont:[UIFont boldSystemFontOfSize:13];
             thisTimeLabel.numberOfLines = 0;             thisTimeLabel.textAlignment = NSTextAlignmentLeft;             thisTimeLabel.text = timeLabelString;             [thisTimeLabel sizeToFit]
         }
                         完成:^(BOOL完)
         {             [个体经营repositionLabels]         }];
        [dayBarArray ADDOBJECT:thisBar];
        [infoLabelArray ADDOBJECT:thisInfoLabel];
        [timeLabelArray ADDOBJECT:thisTimeLabel];
        taDuration = 0;
        tickerCount ++;
        lastBarY =巴里;
        lastHeight = barHeight;
        lastBarFrame = thisBar.frame;        [个体经营positionInitialDayBarView]    }
}


解决方案

确定,第一关。你拒绝使用Interface Builder失去了很多便利的东西。这就像说:我只是使用的是X code 3,因为它曾经为我工作,所以我只是不停它。界面生成器是使创建用户界面非常的简单一个真棒工具。

的实际问题

您说您不使用自动版式。这是不正确的。

默认行为(以iOS8上肯定也iOS7我认为)是,当你创建一个的UIView (或任何UI元素),并把它添加到视图,则该系统将采取自动调整大小面具视图和应用自动布局约束相匹配的大小调整掩码。

这意味着,当你做...

 的UILabel *标签= [[的UILabel页头] initWithFrame:方法CGRectMake(10,10,100,21);
[self.view addSubView:标号];

然后在标签中添加了自动布局约束将其固定的位置(10,10)和大小(100,21)。

您不能那么做改变框...

  label.frame = CGRectMake(10,50,100,21);

由于自动布局约束将只是把它的右后卫在那里开始了。

解决方案

有两个解决办法。

正确解决方案就是学习如何使用自动版式。它已经存在了,现在为3年,而且变得越来越和iOS开发更为重要(甚至只是在肖像iPhone应用程序你现在有4种不同大小的设备来处理)。如果你不开始学习自动版式,你就会落后。这是pretty多的所有iOS发展的需要了。

不正确的,非常非常糟糕解决方案是从添加这些限制停止系统。您可以通过添加该行做到这一点...

  label.translatesAutoresizingMaskIntoConstraints = NO;

创建标签(和所有其他界面元素)。之后

这将停止这些自动生成的约束被添加,并阻止他们与你的布局的东西干扰。

这是在错误解决方案虽然。如果你这样做,你只是在拖延,你要学会自动布局点。

I just switched from iOS 7.1 to iOS 8.1. am encountering the following problem with UIView animateWithDuration. Pictures being worth a thousand words, I made videos of the behavior under each iOS:

iOS 7.1:

https://vimeo.com/113887812

iOS 8.1:

https://vimeo.com/113887814

It's an iPhone app.

I should note that I've searched SO and other places before posting. It seems to be a recognized problem, but what I've found are a few references to "using constraint-based animation" (which I didn't understand) and turning off Auto layout, but no real consensus.

I've tried a couple of stabs, but have gotten nowhere. If someone can point me to (or walk me through) an explicit fix, I'd really appreciate it.

I should mention that the app is intended to work only in the portrait position, so I gave no thought to auto layout during the development process.

All the views involved are created programmatically, except the scroller itself.

Here is the code. I included the whole method, which is pretty long, but I really don't understand what has changed in the new iOS, so am being on the safe side. The animateWithDuration call is pretty deep into the chunk:


-(void) displayDayChart
{
    [self.scroller setBackgroundColor:[UIColor clearColor]];

    UIImage *image = [UIImage imageNamed:@"GrayPaper.png"];
    self.chartView.backgroundColor = [UIColor colorWithPatternImage:image];

    [self.view addSubview:self.scroller];
    [self.scroller setScrollEnabled:YES];

    [self.scroller setContentSize:CGSizeMake(320, 1800)];
    [self.scroller setContentOffset:CGPointMake(0.0, 0.0) animated:YES];

    dayBarArray = [[NSMutableArray alloc]init];
    infoLabelArray = [[NSMutableArray alloc]init];
    timeLabelArray = [[NSMutableArray alloc]init];

    timeFrameDC = - 86400; // 24 hours for this test, selectable in real life

    [self.scroller.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];

    timeframeDCStart = [NSDate dateWithTimeIntervalSinceNow:timeFrameDC];
    timeframeDCEnd = [timeframeDCStart dateByAddingTimeInterval:abs(timeFrameDC)];

    actPredicate = [NSPredicate predicateWithFormat:@"(startTime >= %@ AND stopTime <= %@) OR ((startTime <= %@ AND stopTime >= %@) OR (startTime <= %@ AND stopTime == NULL))",timeframeDCStart,timeframeDCEnd,timeframeDCStart,timeframeDCStart,timeframeDCEnd];

    NSFetchedResultsController *dayActivityFRC = [TimedActivity MR_fetchAllSortedBy:@"startTime" ascending:YES withPredicate:actPredicate groupBy:nil delegate:nil];


    double taDuration;
    double activityPercent;
    int barHeight;
    int lastHeight = 0;
    int tickerCount = 1;
    int barY;
    int lastBarY = 0;
    CGRect lastBarFrame;



#pragma mark DisplayDayChart setup

    for (TimedActivity *activity in dayActivityFRC.fetchedObjects)
    {
        // Create bar and associated labels

        thisBar = [[DayChartBar alloc] initWithFrame:CGRectZero];

        thisInfoLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        thisTimeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        arrowView = [[UIView alloc]initWithFrame:CGRectZero];

        thisBar.tag = tickerCount;

        thisInfoLabel.tag = thisBar.tag + 100;
        thisTimeLabel.tag = thisBar.tag + 200;

        arrowView.tag = thisBar.tag + 300;
        UIImage *image = [UIImage imageNamed:@"LeftArrowRed.png"];


        // Add them to the scroller

        [self.scroller addSubview:thisBar];
        [self.scroller addSubview:thisInfoLabel];
        [self.scroller addSubview:thisTimeLabel];
        [self.scroller addSubview:arrowView];



        NSCalendar *calendar = [NSCalendar currentCalendar];


        if (!activity.stopTime) // Currently running
        {
            if ([activity.startTime timeIntervalSinceDate:timeframeDCStart] < 0)
            {
                dayBarDurationString = @"24:00:00";
                taDuration = (fabs([timeframeDCStart timeIntervalSinceDate:timeframeDCEnd]));
            }

            else
            {
                NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:activity.startTime toDate:timeframeDCEnd options:0];
                NSInteger hours = [components hour];
                NSInteger minutes = [components minute];
                NSInteger seconds =[components second];
                dayBarDurationString = [NSString stringWithFormat:@"%02li:%02li:%02li",(long)hours,(long)minutes,(long)seconds];

                taDuration = (fabs([activity.startTime timeIntervalSinceDate:timeframeDCEnd]));
            }
        }

        else // Either early edge case or fully encapsulated
        {
            if ([activity.startTime timeIntervalSinceDate:timeframeDCStart] < 0) // Early edge case
            {
                NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:timeframeDCStart toDate:activity.stopTime options:0];
                NSInteger hours = [components hour];
                NSInteger minutes = [components minute];
                NSInteger seconds =[components second];
                dayBarDurationString = [NSString stringWithFormat:@"%02li:%02li:%02li",(long)hours,(long)minutes,(long)seconds];

                taDuration = (fabs([activity.stopTime timeIntervalSinceDate:timeframeDCStart]));
            }

            else if ([activity.startTime  timeIntervalSinceDate:timeframeDCStart] > 0) // Encapsulated
            {
                NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:activity.startTime toDate:activity.stopTime options:0];
                NSInteger hours = [components hour];
                NSInteger minutes = [components minute];
                NSInteger seconds =[components second];
                dayBarDurationString = [NSString stringWithFormat:@"%02li:%02li:%02li",(long)hours,(long)minutes,(long)seconds];

                taDuration = [activity.duration doubleValue];
            }

        }


        NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
        [localDateFormatter setDateFormat:@"MMM dd"];

        NSDateFormatter *localTimeFormatter = [[NSDateFormatter alloc] init];
        [localTimeFormatter setDateFormat:@"hh:mm a"];



        NSString * timeLabelDateString = [[NSString alloc]init];

        NSString * timeLabelTimeString = [[NSString alloc]init];

        NSString * timeLabelString = [[NSString alloc]init];


        if (([activity.startTime timeIntervalSinceDate:timeframeDCStart] < 0))
        {
            timeLabelDateString = [localDateFormatter stringFromDate:timeframeDCStart];
            timeLabelTimeString = [localTimeFormatter stringFromDate:timeframeDCStart];
        }

        else
        {
            timeLabelDateString = [localDateFormatter stringFromDate:activity.startTime];
            timeLabelTimeString = [localTimeFormatter stringFromDate:activity.startTime];
        }


        timeLabelString = [NSString stringWithFormat:@"%@ - %@",timeLabelDateString,timeLabelTimeString];

        thisBar.endColor = activity.color;

        activityPercent = fabs((taDuration / timeFrameDC) * 100);

        if (activityPercent > 100)
        {
            activityPercent = 100;
        }

        if (activityPercent < 1.9)
        {
            activityPercent = 1.9;
        }


        barHeight = abs((self.scroller.contentSize.height - 100) * (activityPercent / 100));

        if (tickerCount == 1)
        {
            barY = self.scroller.contentSize.height -10;
        }

        else
        {
            barY = (lastBarY - lastHeight);
        }



#pragma mark DisplayDayChart animation


        [UIView animateWithDuration:.8
                              delay:0.0
                            options: UIViewAnimationCurveEaseInOut // Deprecated, but still works
                         animations:^
         {
             // Starting state *************************************

             thisBar.frame = CGRectMake(20, self.scroller.contentSize.height, 130, 0);
             thisBar.backgroundColor = [UIColor blackColor];

             thisInfoLabel.frame = CGRectMake(20, self.scroller.contentSize.height, thisBar.frame.size.width, 30);

             thisTimeLabel.frame = CGRectMake((thisBar.frame.origin.x) + (thisBar.frame.size.width + 35), self.scroller.contentSize.height, 150, 15);

             thisTimeLabel.textColor = [UIColor blueColor];

             arrowView.frame = CGRectMake(thisTimeLabel.frame.origin.x - 20, thisTimeLabel.frame.origin.y, 16, 16);


             thisInfoLabel.textColor = [UIColor clearColor];


             // End state *************************************

             thisBar.frame = CGRectMake(20, barY, 130, - barHeight);
             thisBar.backgroundColor = thisBar.endColor;

             thisBar.layer.shadowColor = [[UIColor blackColor] CGColor];
             thisBar.layer.frame = CGRectInset(thisBar.layer.frame, 0.0, 3.0);

             thisBar.layer.shadowOpacity = 0.7;
             thisBar.layer.shadowRadius = 4.0;
             thisBar.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);


             // Position infoLabel

             thisInfoLabel.frame = CGRectMake(20, self.scroller.contentSize.height + ((thisBar.frame.size.height / 2) - 30), thisBar.frame.size.width, 30);


             thisTimeLabel.frame = CGRectMake((thisBar.frame.origin.x) + (thisBar.frame.size.width + 35), (thisBar.frame.origin.y) + (thisBar.frame.size.height) - 5, 150, 15);

             thisInfoLabel.textColor = [UIColor blackColor];

             dayBarNameString = activity.name;

             [thisInfoLabel setFont:[UIFont fontWithName:@"Noteworthy-Bold" size:16.0]];

             thisInfoLabel.numberOfLines = 0;

             thisInfoLabel.textAlignment = NSTextAlignmentCenter;
//             thisInfoLabel.layer.borderWidth = 1;


             thisInfoLabel.text = [NSString stringWithFormat:@"%@\n%@",dayBarNameString,dayBarDurationString];

             [thisInfoLabel sizeToFit];


             if ( thisInfoLabel.frame.size.height >= thisBar.frame.size.height)
             {
                 thisInfoLabel.text = [NSString stringWithFormat:@"%@",dayBarNameString];
             }

             else
             {
                 thisInfoLabel.text = [NSString stringWithFormat:@"%@\n%@",dayBarNameString,dayBarDurationString];
             }

             [thisInfoLabel sizeToFit];
             [thisInfoLabel setHidden:NO];

             thisInfoLabel.center = thisBar.center;

             arrowView.frame = CGRectMake(thisTimeLabel.frame.origin.x - 20, thisTimeLabel.frame.origin.y, 16, 16);
             arrowView.backgroundColor = [UIColor colorWithPatternImage:image];

             thisTimeLabel.textColor = [UIColor blueColor];
             [thisTimeLabel setFont:[UIFont boldSystemFontOfSize:13]];
             thisTimeLabel.numberOfLines = 0;

             thisTimeLabel.textAlignment = NSTextAlignmentLeft;

             thisTimeLabel.text = timeLabelString;

             [thisTimeLabel sizeToFit];


         }
                         completion:^(BOOL finished)
         {

             [self repositionLabels];

         }];


        [dayBarArray addObject:thisBar];
        [infoLabelArray addObject:thisInfoLabel];
        [timeLabelArray addObject:thisTimeLabel];
        taDuration = 0;
        tickerCount ++;


        lastBarY = barY;
        lastHeight = barHeight;
        lastBarFrame = thisBar.frame;

        [self positionInitialDayBarView];

    }
}

解决方案

OK, first off. You're losing a lot of convenience stuff by refusing to use Interface Builder. It's like saying "I only use Xcode 3 because it used to work for me so I just kept it.". Interface Builder is an awesome tool that makes creating UIs ridiculously easy.

The actual problem

You said that you are not using AutoLayout. That is not correct.

The default behaviour (in iOS8 definitely and also iOS7 I think) is that when you create a UIView (or any UI element) and add it to a view then the system will take the auto resizing mask of the view and apply Auto Layout constraints to match the resizing mask.

This means that when you do...

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 21)];
[self.view addSubView:label];

Then the label has auto layout constraints added to it to fix it to the position (10, 10) and the size (100, 21).

You can't then change the frame by doing...

label.frame = CGRectMake(10, 50, 100, 21);

Because the auto layout constraints will just put it right back where it started.

Solutions

There are two solutions to this.

The correct solution is to learn how to use AutoLayout. It has been around now for 3 years and is becoming more and more important to iOS development (even just on portrait iPhone apps you now have 4 different devices sizes to deal with). If you don't start learning AutoLayout you will be left behind. It is pretty much a requirement for all iOS development now.

The incorrect and very very bad solution is to stop the system from adding these constraints. You can do this by adding the line...

label.translatesAutoresizingMaskIntoConstraints = NO;

after creating the label (and all other interface elements).

This will stop those automatic constraints from being added and stop them interfering with your layout stuff.

This is the wrong solution though. If you are doing this you are just delaying the point at which you have to learn Auto Layout.

这篇关于UIView的animateWithDuration表现不同的iOS 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆