我猜您正在寻找显示在导航栏下方的上传进度条或类似的东西,无论您去哪个视图,它都会与您同在。几周前我刚刚实现了这个,但有一个额外的警告——我们使用的是 IIViewDeck,所以我们还必须能够处理整个 UINavigationController 的变化。
我所做的是将视图添加为导航栏的子视图。我的导航栏和导航控制器都是子类(并且应用程序已顺利提交到 App Store)。起初我这样做是因为我需要在没有帮助的情况下自定义导航栏,UIAppearance
但现在它似乎是不断给予的礼物,因为它为我提供了相当多的灵活性来完成诸如此类的任务。
- 创建自定义 UINavigationController 和 UINavigationBar
- 如果您需要处理触摸,则必须实现
hitTest:withEvent
- 我也在下面包含了该代码。
- 如果您有一个具有更改
UINavigationController
视图的应用程序(例如选项卡栏应用程序,或者具有可让您更改UINavigationController
位于中心的侧边菜单),我实现了 KVO 来监视中心视图控制器,以便进度视图可以“跟随”它周围。代码也在下面。
这就是我创建自定义 UINavigationController 和 UINavigationBar 的方式:
+ (ZNavigationController *)customizedNavigationController
{
//This is just a regular UINavigationBar subclass - doesn't have to have any code but I personally override the pop/push methods to call my own flavor of viewWill/Did/Appear/Disappear methods - iOS has always been a bit finicky about what gets called when
ZNavigationController *navController = [[ZNavigationController alloc] initWithNibName:nil bundle:nil];
// Ensure the UINavigationBar is created so that it can be archived. If we do not access the
// navigation bar then it will not be allocated, and thus, it will not be archived by the
// NSKeyedArchvier.
[navController navigationBar];
// Archive the navigation controller.
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:navController forKey:@"root"];
[archiver finishEncoding];
// Unarchive the navigation controller and ensure that our UINavigationBar subclass is used.
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setClass:[ZNavigationBar class] forClassName:@"UINavigationBar"];
ZNavigationController *customizedNavController = [unarchiver decodeObjectForKey:@"root"];
[unarchiver finishDecoding];
// Modify the navigation bar to have a background image.
ZNavigationBar *navBar = (ZNavigationBar *)[customizedNavController navigationBar];
[navBar setTintColor:kZodioColorBlue];
[navBar setBackgroundImage:[UIImage imageNamed:@"Home_TopBar_RoundCorner_withLogo"] forBarMetrics:UIBarMetricsDefault];
return customizedNavController;
}
现在您已经有了自己的自定义ZNavigationBar
(这些是我的代码中的实际名称 - 很难通过并全部更改它们)您实现了触摸处理 - 变量名称非常容易解释 - 我想检测“左侧”中的触摸此进度条的“附件视图”、“主区域”或“右附件视图”:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
DDLogVerbose(@"Got a touch at point X: %f Y: %f", point.x, point.y);
//First check if the progress view is visible and touch is within that frame
if ([[JGUploadManager sharedManager] progressViewVisible] &&
CGRectContainsPoint([[JGUploadManager sharedManager] uploadProgressView].frame, point))
{
//Modified frame - have to compensate for actual position of buttons in the view from the standpoint of the UINavigationBar. Can probably use another/smarter method to do this.
CGRect modifiedCancelButtonFrame = [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryFrame;
modifiedCancelButtonFrame.origin.y += [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView.superview.frame.origin.y;
//Do the same thing for the right view
CGRect modifiedRetryButtonFrame = [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryFrame;
modifiedRetryButtonFrame.origin.y += [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView.superview.frame.origin.y;
//Touch is on the left button
if ([[JGUploadManager sharedManager] progressViewVisible] &&
[[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView &&
CGRectContainsPoint(modifiedCancelButtonFrame, point))
{
return [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView;
}
//Touch is on the right button
else if ([[JGUploadManager sharedManager] progressViewVisible] &&
[[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView &&
CGRectContainsPoint(modifiedRetryButtonFrame, point))
{
return [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView;
}
//Touch is in the main/center area
else
{
return [[JGUploadManager sharedManager] uploadProgressView];
}
}
//Touch is not in the progress view, pass to super
else
{
return [super hitTest:point withEvent:event];
}
}
现在是“跟随”导航控制器的 KVO 部分。在某处实现 KVO - 我使用单例“上传管理器”类,并将其放在该类的 init 中,但这取决于您的应用程序的设计:
ZRootViewController *rootViewController = ((JGAppDelegate*)[UIApplication sharedApplication].delegate).rootViewDeckController;
[rootViewController addObserver:sharedManager
forKeyPath:@"centerController"
options:NSKeyValueObservingOptionNew
context:nil];
然后当然实现这个:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
DDLogVerbose(@"KVO shows something changed: %@", keyPath);
if ([keyPath isEqualToString:@"centerController"])
{
DDLogVerbose(@"Center controller changed to: %@", object);
[self centerViewControllerChanged];
}
}
最后是centerViewControllerChanged
函数:
- (void)centerViewControllerChanged
{
ZRootViewController *rootViewController = ((JGAppDelegate*)[UIApplication sharedApplication].delegate).rootViewDeckController;
ZNavigationController *centerController = (ZNavigationController *)rootViewController.centerController;
//Check if the upload progress view is visible and/or active
if (self.uploadProgressView.frame.origin.y > 0 && self.uploadProgressView.activityIndicatorView.isAnimating)
{
[centerController.navigationBar addSubview:self.uploadProgressView];
}
//Keep weak pointer to center controller for other stuff
DDLogVerbose(@"Setting center view controller: %@", centerController);
self.centerViewController = centerController;
}
如果我误解了您的问题,那么我只是输入了世界上最长的 SO 答案,但徒劳无功。我希望这会有所帮助,如果有任何部分需要澄清,请告诉我!