在导航控制器下添加UIProgressView

时间:2013-11-16 13:19:56

标签: ios objective-c

这似乎与这个问题类似(Showing a UIProgressView inside or on top of a UINavigationController's UINavigationBar),但是如果没有Xcode抛出一些可怕的错误,我无法得到适合我的答案。

我有类似的问题,但我想在分割视图控制器的主视图上添加进度条,所以它是一个tableview,但上面的答案中给出的代码不起作用(我不可以用同样的方式访问导航栏。

我已经尝试过这样做了:

    UIProgressView *progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];;
[progress setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.navigationController.navigationBar addSubview:progress];

这几乎可以工作,但它将它添加到视图上方而不是下方和半宽(恼人地)。我可以在故事板中执行此操作,但是当我将新的tableview动态推送到导航堆栈时,它会再次消失。

我的目标是:

Progressview Under Nav

感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

override func viewDidLoad() {
    super.viewDidLoad()

    // if VC is pushed in a navigation controller I add a progress bar
    if let navigationVC = self.navigationController {

        // create progress bar with .bar style and add it as subview
        let progressBar = UIProgressView(progressViewStyle: .Bar)
        navigationVC.navigationBar.addSubview(progressBar)

        // create constraints
        // NOTE: bottom constraint has 1 as constant value instead of 0; this way the progress bar will look like the one in Safari
        let bottomConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Bottom, relatedBy: .Equal, toItem: progressBar, attribute: .Bottom, multiplier: 1, constant: 1)
        let leftConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Leading, relatedBy: .Equal, toItem: progressBar, attribute: .Leading, multiplier: 1, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Trailing, relatedBy: .Equal, toItem: progressBar, attribute: .Trailing, multiplier: 1, constant: 0)

        // add constraints
        progressBar.translatesAutoresizingMaskIntoConstraints = false
        navigationVC.view.addConstraints([bottomConstraint, leftConstraint, rightConstraint])
    }
}

答案 1 :(得分:0)

这项技术对我来说很好。除了将其添加到导航栏之外,添加约束使其保持在底部上方.5或1像素并将其固定到左侧和右侧。然后它也适用于景观。

在我的例子中,我使用了一个nav控制器子类,让它添加并隐藏进度控件,然后提供方便的方法来允许任何视图控制器访问它。

编辑:自2013年以来我没有编译或使用过代码。它在导航控制器子类中:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UIProgressView *progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progress.tag = DISPLAY_PROGRESS_VIEW;
[self.view addSubview:progress];
UINavigationBar *navBar = [self navigationBar];

NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:progress attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeBottom multiplier:1 constant:-0.5];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:progress attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:progress attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeRight multiplier:1 constant:0];
[self.view addConstraint:constraint];

[progress setTranslatesAutoresizingMaskIntoConstraints:NO];
progress.hidden = YES;

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[navBar addGestureRecognizer:longPressGesture];
}

答案 2 :(得分:0)

// Add progress view to bar
myProgressView.frame = CGRect(x: 0, y: 64, width: self.view.frame.width, height: 4)
myProgressView.alpha = 0
myProgressView.backgroundColor = Color.dark()
myProgressView.tintColor = UIColor.white

navigationController!.view.addSubview(myProgressView)