【问题标题】:increase UIProgressView height when using it as titleView in navigation bar将 UIProgressView 用作导航栏中的 titleView 时增加其高度
【发布时间】:2014-05-20 15:46:20
【问题描述】:

我在导航项中使用进度视图 (UIProgressView) 作为 titleView,以便它出现在我的导航栏中。无论我做什么,它在 iOS 7.1 中都显得非常狭窄:

我讨厌这个版本的进度视图。如何让它更胖/更厚/更高?我已经尝试了herehere 的所有建议。我尝试过更改框架、添加转换、使用自定义子类、添加约束(这些只会导致崩溃)。而我以前使用的技巧,添加progressImagetrackImage,是broken in iOS 7.1

这是我的代码(整个事情都在代码中发生;我在这个项目中没有 nib 或情节提要):

UIProgressView* prog = [[UIProgressView alloc] init];
self.prog = prog;
self.prog.progressTintColor = [UIColor colorWithRed:1.000 green:0.869 blue:0.275 alpha:1.000];
self.prog.trackTintColor = [UIColor darkGrayColor];
self.navigationItem.titleView = prog;

【问题讨论】:

    标签: ios ios7 uiprogressview


    【解决方案1】:

    我以一种特别棘手的方式解决了这个问题。我们已经知道提供高度约束应该有效。但只是这样做会使我的应用程序崩溃。最后我有一个头脑风暴。我使用 another 视图作为我的titleView,并将进度视图放在其中。现在我能够应用适当的约束:

    UIProgressView* prog = [[UIProgressView alloc] init];
    self.prog = prog;
    self.prog.progressTintColor = [UIColor colorWithRed:1.000 green:0.869 blue:0.275 alpha:1.000];
    self.prog.trackTintColor = [UIColor darkGrayColor];
    self.prog.translatesAutoresizingMaskIntoConstraints = NO;
    CGFloat w = 150;
    CGFloat h = 10;
    [self.prog addConstraint:[NSLayoutConstraint constraintWithItem:self.prog attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:w]];
    [self.prog addConstraint:[NSLayoutConstraint constraintWithItem:self.prog attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:h]];
    UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0,0,w,h)];
    [v addSubview:self.prog];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:self.prog attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:v attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:self.prog attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:v attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    v.clipsToBounds = YES;
    v.layer.cornerRadius = 4;
    self.navigationItem.titleView = v;
    

    如您所见,我还利用这一点为角落添加了一些圆角。我觉得结果很好看。

    【讨论】:

    • 仅供参考。我不得不删除宽度限制,因为我在带有 Xcode 6.3.1 的 IOS 8 中有一个警告,但它甚至适用于圆角。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2018-03-01
    • 2015-11-03
    • 2017-10-25
    相关资源
    最近更新 更多