【问题标题】:Custom UIProgressView in iOS7 not possible?无法在 iOS7 中自定义 UIProgressView?
【发布时间】:2013-09-29 11:34:02
【问题描述】:

我知道这已经在这里被问了大约一百次了,但是我在其他问题中找不到合适的答案。

我的问题是 UIProgressView 的高度。虽然在 iOS6 中一切正常,但现在在 iOS7 中一切正常。

我尝试了以下方法:

  • 1.在drawRect-Method中设置自定义布局:

在 iOS6 中就像一个魅力,但在 iOS7 中,进度从一开始就设置为 100%,或者进度条非常细。

  • 2.使用UIProgressView外观的progressImagetrackImage属性设置布局

iOS7 下也不行。这里的进度条也从一开始就设置为 100%。有人写道,这种方式应该是可能的,但对于 iOS7,我无法确认。

  • 3.使用initWithProgressStyle进行初始化,然后设置进度视图的框架

在 iOS6 和 iOS7 下不适合我。在 iOS7 中,这些条非常细。

对我来说,现在这很令人沮丧,因为这些条要么是 100%,要么是超细的。谁能给我一个建议,以达到我的进度视图的旧布局。我认为这必须是可能的,因为如果我在 iPhone(安装了 iOS7)上查看我的 Spotify 应用程序,进度视图看起来就像以前一样。

非常感谢!

【问题讨论】:

  • 不幸的是,iOS7 API 引入了许多 UI 限制,并迫使开发人员将他们的应用程序设计为与现有应用程序完全一样。再见创意。好消息是您始终可以编写自己的视图并创建自己的控件,而不受标准控件的限制。为我们做更多的工作。
  • 大声笑@“再见创造力”
  • 我会说“再见原创性”。在我看来,从 iOS 7 的约束指南中为您的应用找到理想的视觉设计是一种创造力。

标签: objective-c ios7 uiprogressview


【解决方案1】:

我为这个问题做了一种解决方法。我希望有人可以为正常的 UIProgressView 给出一个很好的答案。

我写了一个带有圆角的 UIView 子类和一个里面的视图,它根据给定的进度改变它的大小。我只使用颜色作为背景,但图像也可以。代码如下:

#import "CustomProgressView.h"
#import <QuartzCore/QuartzCore.h>

@interface CustomProgressView ()

@property (nonatomic, retain) UIView *progressView;

@end

@implementation CustomProgressView

@synthesize progressColor,trackColor,progressView,progress;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 5;
        // clipsToBounds is important to stop the progressView from covering the original view and its round corners
        self.clipsToBounds = YES;

        self.progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
        [self addSubview:self.progressView];
    }

    return self;
}

-(void)setProgressColor:(UIColor *)theProgressColor {
    self.progressView.backgroundColor = theProgressColor;
    progressColor = theProgressColor;
}

-(void)setTrackColor:(UIColor *)theTrackColor {
    self.backgroundColor = theTrackColor;
    trackColor = theTrackColor;
}

-(void)setProgress:(float)theProgress {
    progress = theProgress;
    CGRect theFrame = self.progressView.frame;
    theFrame.size.width = self.frame.size.width * theProgress;
    self.progressView.frame = theFrame;
}
@end

【讨论】:

    【解决方案2】:

    嗯,问题是 iOS6 UIProgressView 和 iOS7 UIProgressView 有不同的内部子视图结构的接缝。 iOS6 进度视图是没有子视图(或一些次要视图)的单一视图,iOS7 进度视图很少有用于绘制进度条和背景的额外子视图。

    如果您在 iOS7 上删除 UIProgressView 的所有子视图,那么 drawRect: 方法在 iOS6 上的工作方式与以前相同,但您将完全负责绘制进度视图内容,包括进度条和背景。

    - (id) initWithCoder: (NSCoder*)aDecoder
    {
        if(self=[super initWithCoder: aDecoder])
        {
                // Also you can setup height of your progress here
                // self.frame = CGRectMake(0,0,100,yourHeight);
    
            NSArray *subViews = self.subviews;
            for(UIView *view in subViews)
            {
                [view removeFromSuperview];
            }
        }
        return self;
    }
    

    【讨论】:

      【解决方案3】:

      我有一个自定义 UIProgressView,它自己的 drawRect 是从后台进程更新的。 在 iOS6 中一切正常,而在 iOS7 中,进度条没有更新。

      我像这样在setProgress 之后添加了layoutSublayersOfLayer

      [self.loadingProgress setProgress:pv.floatValue];
      [self.loadingProgress layoutSublayersOfLayer:self.loadingProgress.layer];
      

      它就像一个魅力。

      我希望这对某人有所帮助。

      【讨论】:

        【解决方案4】:

        避免头痛并使用这个优秀的库:

        YLProgressBar

        1. 从 YLProgressBar 文件夹中复制 YLProgressBar.h 和 YLProgressBar.m。
        2. #import "YLProgressBar.h" 在你想使用进度条的文件中
        3. 通过代码或 xib 添加进度条
        4. progressBar.type = YLProgressBarTypeRounded; progressBar.progressTintColor = [UIColor greenColor]; progressBar.stripesOrientation = YLProgressBarStripesOrientationVertical; progressBar.stripesDirection = YLProgressBarStripesDirectionLeft;
        5. 您有一个不错的、功能齐全的进度条,它支持宽度、高度、修改。

        【讨论】:

        • 感谢您的建议!图书馆看起来很不错。在某些情况下,虽然它不想使用外部库,例如由客户。特别是如果不需要它们来获得想要的功能。
        • 不要只发布图书馆的链接。展示如何使用它(根据问题量身定制)。否则,这只是一个仅链接的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多