- 我在 AUIController 中,导航控制器.导航栏.alpha == 0.500
- 我按下了主页按钮。
- 我点击了appIcon回到AUIController。 导航控制器.导航栏.alpha 到 1.000
我该怎么做才能保持导航控制器.导航栏的 alpha = 0.5000 ;
我试过..
AUIController : UIViewController <...,UINavigationControllerDelegate>
- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController == self) {
//NSLog(@"self");
//self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
self.navigationController.navigationBar.alpha = 0.500;
self.navigationController.navigationBar.translucent = YES;
} else {
self.navigationController.navigationBar.alpha = 1.000;
self.navigationController.navigationBar.translucent = NO;
}
}
但是当应用程序变得活跃时,alpha 0.50 变为 1.00将不调用显示视图控制器
您可以使用 KVO 来执行此操作。viewDidLoad
AUIController
添加此代码[self.navigationController.navigationBar addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:(__bridge void *)(self)];
并实现此功能:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog([change description]);
UIViewController *viewController = (__bridge UIViewController*)context;
if ([viewController isKindOfClass:[AUIViewController class]]) {
if (self.navigationController.navigationBar.alpha == 1) {
self.navigationController.navigationBar.alpha = 0.500;
self.navigationController.navigationBar.translucent = YES;
}
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
self.navigationBar.translucent = NO;
[self.navigationController.layer removeFromSuperlayer];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
self.navigationBar.translucent = YES;
}
只需在方法中写下以下代码
AppDelegate.m 文件。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
int count=self.navigationController.viewControllers.count;
if([[[self.navigationController viewControllers] objectAtIndex:count-1] isKindOfClass:[AUIController Class]])
{
self.navigationController.navigationBar.alpha = 0.500;
self.navigationController.navigationBar.translucent = YES;
}
else
{
self.navigationController.navigationBar.alpha = 1.000;
self.navigationController.navigationBar.translucent = NO;
}
}
我希望这对你有帮助。