iOS 設置導航欄和狀態欄為透明黑色


第一種方法:

//    導航欄變為透明
[self.navigationController.navigationBar setBackgroundImage:[self ImageWithColor:[[UIColor blackColor]colorWithAlphaComponent:0.4]] forBarMetrics:0]; 
// 讓黑線消失的方法 
self.navigationController.navigationBar.shadowImage=[UIImage new];

    //設置了setBackgroundImage,再設置barTintColor不起作用

//    self.navigationController.navigationBar.barTintColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];

 

//將color轉換成image方法

- (UIImage *)ImageWithColor:(UIColor *)backgroundColor {

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, backgroundColor.CGColor);

    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

第二種方法:

    UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, -20, KScreenWidth, kNavHeight)];
    bg.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    bg.userInteractionEnabled = YES;
    [self.navigationController.navigationBar addSubview:bg];
    [self.navigationController.navigationBar sendSubviewToBack:bg];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM