2014-12-02 15 views
8

Trong chức năng didFinishLaunchingWithOptions của đại biểu ứng dụng của tôi, tôi cố gắng tùy chỉnh giao diện của thanh điều hướng của tôi.Làm cách nào để đặt hình ảnh có độ bóng cao 1px mờ cho UINavigationBar?

[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) 
]; 

Tôi mong đợi một bóng đỏ đục 1px cao. Thay vào đó, tôi được cho một cái bóng màu đỏ mờ 2px. Làm thế nào có thể làm cho nó xuất hiện chính xác như tôi muốn nó? Tôi đã thực hiện các thiết lập ngoại hình tương tự cho UITabBar. Mặt khác, nó hoạt động rất tốt.

enter image description here

Chức năng loại đó tạo ra hình ảnh động được định nghĩa như vậy:

+ (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; 
} 
+0

có bạn nhìn vào chỉ thêm một cái bóng đến CALayer? xem http://stackoverflow.com/questions/805872/how-do-i-draw-a-shadow-under-a-uiview – cmyr

+0

@cmyr '[UINavigationBar xuất hiện] .layer.borderWidth = 0.5f; [UINavigationBar xuất hiện] .layer.borderColor = [UIColor redColor] .CGColor; 'không hiển thị gì bên dưới thanh điều hướng. – Pwner

+0

bạn đã đặt clipToBounds của lớp thành KHÔNG? – cmyr

Trả lời

-1

Uh ... được này những gì bạn muốn?

screenshot

Trong điều khiển quan điểm của tôi, tôi chỉ cần thêm một UIView với 1 chiều cao pixel và một bù đắp của 44pixels:

- (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]; 
} 

Bạn có thể thay đổi màu sắc để bất cứ điều gì bạn muốn.

+2

IMHO Tôi nghĩ rằng đó không phải là giải pháp tốt nhất để thêm một subview vào navigationBar. Dường như lỗi dễ xảy ra (ví dụ: khi nhiều bộ điều khiển chế độ xem cố gắng thêm chế độ xem hoặc - đặc biệt - khi phân cấp chế độ xem cho thanh điều hướng có thể thay đổi với phiên bản cập nhật của iOS). Tôi nghĩ rằng an toàn hơn nhiều khi sử dụng các chức năng công khai có sẵn, thay vào đó là hình nền tùy chỉnh và cài đặt UIAppearance. – auco

15

Bạn cần phải tạo ra một bối cảnh đồ họa đó là võng mạc-aware:

let pixelScale = UIGraphicsBeginImageContextWithOptions(fillRect.size, false, UIScreen.main.scale) 

Sau đó, hình ảnh bóng của bạn sẽ trở thành cao 1 pixel vật lý.

Đây là mã đầy đủ:

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 
    } 

} 

On GitHub:

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

+0

Tôi muốn đề xuất tạo kích thước lấp đầy {1, pixelSize} thay vì {pixelSize, pixelSize}, nếu không nó sẽ tạo ra hiệu ứng gradient ngang và dòng trở nên trong suốt tại điểm bên phải trong trường hợp của tôi. –

Các vấn đề liên quan