如何为UINavigationBar设置不透明的1px高阴影图像?

Pwn*_*ner 9 cocoa-touch uinavigationbar ios uiappearance

在我的应用程序委托的didFinishLaunchingWithOptions函数中,我尝试自定义导航栏的外观.

[UINavigationBar appearance].translucent = NO;
[[UINavigationBar appearance] 
    setBackgroundImage:[UIImage 
        imageWithColor:[UIColor whiteColor] 
        size:CGSizeMake(1.0f, 1.0f)
    ] 
    forBarMetrics:UIBarMetricsDefault
];
[UINavigationBar appearance].shadowImage = [UIImage 
    imageWithColor:[UIColor redColor] 
    size:CGSizeMake(0.5f, 0.5f)
];
Run Code Online (Sandbox Code Playgroud)

我期待一个1px高的不透明红色阴影.相反,我给了一个2px高的半透明红色阴影.怎么能让它看起来像我想要的那样?我已经对UITabBar进行了类似的外观设置.另一方面,它表现得很好.

在此输入图像描述

创建动态图像的类别函数定义如下:

+ (UIImage*)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
Run Code Online (Sandbox Code Playgroud)

Rud*_*vič 17

您需要创建一个视网膜感知的图形上下文:

let pixelScale = UIGraphicsBeginImageContextWithOptions(fillRect.size, false, UIScreen.main.scale)
Run Code Online (Sandbox Code Playgroud)

之后,您的阴影图像将变为1个物理像素高.

这是完整的代码:

extension UIImage {

    static func imageWithColor(color: UIColor) -> UIImage {
        let pixelScale = UIScreen.main.scale
        let pixelSize = 1 / pixelScale
        let fillSize = CGSize(width: pixelSize, height: pixelSize)
        let fillRect = CGRect(origin: CGPoint.zero, size: fillSize)
        UIGraphicsBeginImageContextWithOptions(fillRect.size, false, pixelScale)
        let graphicsContext = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(graphicsContext, color.CGColor)
        CGContextFillRect(graphicsContext, fillRect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

}
Run Code Online (Sandbox Code Playgroud)

在GitHub上:

https://github.com/salutis/swift-image-with-color


Zha*_*ang -2

呃……这就是你想要的吗?

截屏

在我的视图控制器中,我简单地添加了一个高度为 1 像素、偏移量为 44 像素的 UIView:

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

    self.view.backgroundColor = [UIColor whiteColor];

    [self setupNavBar];
}

-(void)setupNavBar
{
    self.navigationItem.title = @"Contacts";

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 44, applicationFrame.size.width, 1.0)];
    bar.backgroundColor = [UIColor redColor];

    [self.navigationController.navigationBar addSubview:bar];
}
Run Code Online (Sandbox Code Playgroud)

您可以将颜色更改为您想要的任何颜色。

  • 恕我直言,我认为这不是向导航栏添加子视图的最佳解决方案。这似乎很容易出错(例如,当多个视图控制器尝试添加视图时,或者 - 特别是 - 当导航栏的视图层次结构可能会随着 iOS 的更新版本而改变时)。我认为使用公开可用的功能、自定义背景图像和 UIAppearance 设置更安全。 (3认同)