iOS7 - Change UINavigationBar border color

后端 未结 15 2050
时光取名叫无心
时光取名叫无心 2020-12-07 14:44

Is it possible to change the grey border-bottom color of the UINavigationBar in iOS7?

I already tried to remove to border, but this is not working:

[         


        
相关标签:
15条回答
  • 2020-12-07 15:31

    This will help you :)

    [self.navigationController.navigationBar.layer setBorderWidth:2.0];// Just to make sure its working
    [self.navigationController.navigationBar.layer setBorderColor:[[UIColor redColor] CGColor]];
    
    0 讨论(0)
  • 2020-12-07 15:31

    Here's the method for creating image with clear color:

    + (UIImage*)imageFromColor:(UIColor *)color withSize:(CGSize)sizeImage
    {
        UIImage *resultImage = nil;
    
        UIGraphicsBeginImageContext(sizeImage);
    
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor);
        CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0.0f, 0.0f, sizeImage.width, sizeImage.height));
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return resultImage;
    }
    

    Here's it's usage for removing annoying bottom line:

    navigationBar.shadowImage = [UIImage imageFromColor:[UIColor clearColor] withSize:CGSizeMake(1.0f, 1.0f)];
    
    0 讨论(0)
  • 2020-12-07 15:34

    To build on @sash's Swift implementation you can make the border responsive to rotation/trait changes by using constraints:

    extension UINavigationBar {
      func setBottomBorderColor(color: UIColor, height: CGFloat) {
    
        let bottomBorderView = UIView()
        bottomBorderView.backgroundColor = color
        bottomBorderView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(bottomBorderView)
    
        // Add constraints to make the bar always stay at the bottom of the nav bar and change size with rotation/trait changes
        let horizontalConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
        let verticalConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
        let widthConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .width, multiplier: 1, constant: 0)
        let heightConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: height)
    
        self.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
      }
    }
    
    0 讨论(0)
提交回复
热议问题