Ios 更改导航栏的alpha值
这可能吗Ios 更改导航栏的alpha值,ios,objective-c,uiview,uinavigationcontroller,uinavigationbar,Ios,Objective C,Uiview,Uinavigationcontroller,Uinavigationbar,这可能吗 我想更改视图控制器(动画中)中导航栏的alpha值,但如果我更改了self.navigationController.navigationBar.alpha=0.0,导航栏占据的屏幕部分完全消失,留下一个黑框,这不是我想要的(我更喜欢它是self.view背景的颜色)。直接来自Apple开发者参考: “只有少数几个直接定制可供您使用 导航栏。具体来说,可以修改栏样式, tintColor和半透明属性,但决不能直接 更改ui视图级别的属性,例如帧,边界, alpha,或直接隐藏属性。”
我想更改视图控制器(动画中)中导航栏的alpha值,但如果我更改了self.navigationController.navigationBar.alpha=0.0,导航栏占据的屏幕部分完全消失,留下一个黑框,这不是我想要的(我更喜欢它是
self.view
背景的颜色)。直接来自Apple开发者参考:
“只有少数几个直接定制可供您使用
导航栏。具体来说,可以修改栏样式
,
tintColor
和半透明
属性,但决不能直接
更改ui视图
级别的属性,例如帧
,边界
,
alpha
,或直接隐藏属性。”
但是,可以设置导航栏的半透明属性。如果您执行[self.navigationController.navigationBar settransparent:YES]代码>
应该能解决你的问题。您还可以尝试查看是否有任何UIBarStyle
enum是您想要的。这将为您提供所需的效果:
self.navigationController.navigationBar.alpha = 0.0;
self.navigationController.navigationBar.frame = CGRectMake(0,0,320,0);
没有在动画中尝试此操作,但在我的视图中有效,希望它有效。如果您只是想以动画方式摆脱导航栏,您可以执行以下操作:
[self.navigationController setNavigationBarHidden:YES animated:YES];
如果您想要控制动画,并且需要将alpha
设置为0.0
,请继续阅读:
您看到的“黑盒”来自底层视图或窗口。如果您只想查看颜色而不是“黑框”,请执行以下操作:
如果希望实际视图位于导航栏所在的位置,则需要增加视图的高度
:
[UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{
self.navigationController.navigationBar.alpha = 0.0;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGRect frame = self.view.frame;
frame.origin.y -= navigationBarHeight;
frame.size.height += navigationBarHeight;
self.view.frame = frame;
} completion:NULL];
由于我支持Colin的回答,我想给您一个额外的提示,定制UINavigationBar的外观,包括alpha
诀窍是在导航栏中使用ui外观。这使您能够将UIImage分配给导航栏的背景图像。您可以通过编程方式生成这些UIImages,并将其用于UIColors,并根据需要设置颜色的alpha属性。我已经在自己的一个应用程序中完成了这项工作,它的工作原理与预期相符
这里我给你一些代码片段:
例如,在..AppDelegate.m中,在didFinishLaunchingWithOptions中添加以下行:
//create background images for the navigation bar
UIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientation
UIImage *gradientImage32 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape orientation
//customize the appearance of UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
// create background images for the navigation bar
let gradientImage44 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
let gradientImage32 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
// customize the appearance of UINavigationBar
UINavigationBar.appearance().setBackgroundImage(gradientImage44, forBarMetrics: .Default)
UINavigationBar.appearance().setBackgroundImage(gradientImage32, forBarMetrics: .Compact)
UINavigationBar.appearance().barStyle = .Default
实现方便的方法以编程方式创建UIImage对象,例如为UIImage创建新类别:
//UIImage+initWithColor.h
//
#import <UIKit/UIKit.h>
@interface UIImage (initWithColor)
//programmatically create an UIImage with 1 pixel of a given color
+ (UIImage *)imageWithColor:(UIColor *)color;
//implement additional methods here to create images with gradients etc.
//[..]
@end
//UIImage+initWithColor.m
//
#import "UIImage+initWithColor.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIImage (initWithColor)
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
// create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我创建了一个小的演示项目,并为您添加了两个屏幕截图:
视图本身具有黄色背景色。
导航栏的背景图像为红色。
屏幕截图1显示了一个导航栏,其alpha值为0.2。
屏幕截图2显示了一个导航栏,其alpha值为0.8
您有一些选项,部分取决于UINavigationBar
的条形样式。最重要的是要意识到,您可能不必为alpha属性设置动画以获得所描述的效果
UIBarStyleDefault
或UIBarStyleBlackOpaque
选项A是将UINavigationBar半透明属性设置为YES,然后设置alpha动画:
navigationBar.translucent = YES; // ideally set this early, in the nib/storyboard, or viewDidLoad
...
[UIView animateWithDuration: 1.0
animations: ^{
// toggle:
navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0;
}];
[UIView animateWithDuration: 1.0
animations: ^{
// toggle:
navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0;
}];
在这种情况下,视图将位于导航栏后面,即使其alpha为1.0。这种情况的缺点是,即使使用1.0 alpha,您也可能会在UINavigationBar后面看到视图背景色的色调。此外,所有子视图都需要从顶部向下放置44个点
UIBarStyleDefault
或UIBarStyleBlackOpaque
选项B用于在交叉分解转换动画中隐藏导航栏。这将显示UINavigationBar
的超级视图。如果您使用的是UINavigationController
,则您将看到UINavigationController
视图的黑色背景-但您可以设置UINavigationController
视图的背景色,以匹配您的视图,以获得您想要的效果:
UINavigationBar* navigationBar = self.navigationController.navigationBar;
self.navigationController.view.backgroundColor = self.view.backgroundColor;
[UIView transitionWithView: navigationBar
duration: 1.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations: ^{
// toggle:
navigationBar.hidden = !navigationBar.hidden;
}
completion: nil];
如果由于隐藏了UINavigationBar
,因此UINavigationController
更新了您的图幅,则此解决方案需要注意的一点可能是布局问题。这很好,只是如果子视图定位在左上角,子视图可能会向上移动44个像素。为了解决这个问题,你可以考虑将你的子视图锚定到你的视图底部(或者是用弹簧或者用布局约束)。
UIBarStyleDefault
或UIBarStyleBlackOpaque
选项C将用另一个视图覆盖UINavigationBar
,再次使用交叉分解转换动画:
UINavigationBar* navigationBar = self.navigationController.navigationBar;
[UIView transitionWithView: navigationBar
duration: 1.0
options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent
animations: ^{
// toggle:
const int tag = 1111;
UIView* navOverlayView = [navigationBar viewWithTag: tag];
if ( navOverlayView == nil )
{
navOverlayView = [[UIView alloc] initWithFrame: CGRectInset( navigationBar.bounds, 0, -3 ) ];
navOverlayView.backgroundColor = self.view.backgroundColor;
navOverlayView.tag = tag;
[navigationBar addSubview: navOverlayView];
}
else
{
[navOverlayView removeFromSuperview];
}
}
completion: nil];
uibarstyleblacktransparent:此选项最简单,因为UINavigationBar
已经是半透明的,并且您的视图已经在其后面。只需设置alpha的动画:
navigationBar.translucent = YES; // ideally set this early, in the nib/storyboard, or viewDidLoad
...
[UIView animateWithDuration: 1.0
animations: ^{
// toggle:
navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0;
}];
[UIView animateWithDuration: 1.0
animations: ^{
// toggle:
navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0;
}];
米克布朗在斯威夫特的回答:
在AppDelegate.swift中,在didFinishLaunchingWithOptions中添加以下行:
//create background images for the navigation bar
UIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientation
UIImage *gradientImage32 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape orientation
//customize the appearance of UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
// create background images for the navigation bar
let gradientImage44 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
let gradientImage32 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
// customize the appearance of UINavigationBar
UINavigationBar.appearance().setBackgroundImage(gradientImage44, forBarMetrics: .Default)
UINavigationBar.appearance().setBackgroundImage(gradientImage32, forBarMetrics: .Compact)
UINavigationBar.appearance().barStyle = .Default
实现方便的方法以编程方式创建UIImage对象
class func imageWithColor(colour: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
// Create a 1 by 1 pixel content
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
colour.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
最直接的方法是修改navigationBar背景视图的alpha组件,此时(iOS9)是第一个navigationBar子视图。但是请注意,我们永远不知道苹果是否会在以后的版本中更改子视图层次结构,所以必须小心
let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
navigationBackgroundView?.alpha = 0.7
您还可以尝试在导航栏下方设置背景视图,并直接修改此视图
private lazy var backgroundView: UIView = UIView()
...
private func setupNavigationBarBackground() {
guard let navigationBar = navigationController?.navigationBar else { return }
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.isTranslucent = true
view.addSubview(backgroundView)
backgroundView.alpha = 0
backgroundView.backgroundColor = .red
backgroundView.translatesAutoresizingMaskIntoConstraints = false
backgroundView.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor).isActive = true
backgroundView.bottomAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
backgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
backgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
}
private func changeNavigationBarAlpha(to alpha: CGFloat) {
backgroundView.alpha = alpha
}
Swift3
var navAlpha = // Your appropriate calculation
self.navigationController!.navigationBar.backgroundColor = UIColor.red.withAlphaComponent(navAlpha)
“半透明”选项不起作用,它似乎只是使导航栏稍微轻一点。@DougSmith是的,但是Doug,当你将导航栏设置为半透明时,视图的大小就会变大