iOS7 - Change UINavigationBar border color

后端 未结 15 2035
时光取名叫无心
时光取名叫无心 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:07

    Here is a category to change bottom color with height:

    [self.navigationController.navigationBar setBottomBorderColor:[UIColor redColor] height:1];
    

    Objective C:

    UINavigationBar+Helper.h

    #import <UIKit/UIKit.h>
    
    @interface UINavigationBar (Helper)
    - (void)setBottomBorderColor:(UIColor *)color height:(CGFloat)height;
    @end
    

    UINavigationBar+Helper.m

    #import "UINavigationBar+Helper.h"
    
    @implementation UINavigationBar (Helper)
    
    - (void)setBottomBorderColor:(UIColor *)color height:(CGFloat)height {
        CGRect bottomBorderRect = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), height);
        UIView *bottomBorder = [[UIView alloc] initWithFrame:bottomBorderRect];
        [bottomBorder setBackgroundColor:color];
        [self addSubview:bottomBorder];
    }
    @end
    

    Swift:

    extension UINavigationBar {
    
        func setBottomBorderColor(color: UIColor, height: CGFloat) {
            let bottomBorderRect = CGRect(x: 0, y: frame.height, width: frame.width, height: height)
            let bottomBorderView = UIView(frame: bottomBorderRect)
            bottomBorderView.backgroundColor = color
            addSubview(bottomBorderView)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 15:07

    budidino solutions works very well. Here it is for Swift:

    let navBarLineView = UIView(frame: CGRectMake(0,
        CGRectGetHeight((navigationController?.navigationBar.frame)!),
        CGRectGetWidth((self.navigationController?.navigationBar.frame)!),
        1))
    
    navBarLineView.backgroundColor = UIColor.whiteColor()
    
    navigationController?.navigationBar.addSubview(navBarLineView)
    
    0 讨论(0)
  • 2020-12-07 15:08

    I'm using RubyMotion with the RedPotion gem, which includes a StandardAppearance class. This is what I did!

    Put this line at the top of your app_delegate.rb, just before the on_load method:

    ApplicationStylesheet.new(nil).application_setup
    

    Then, in your application_stylesheet.rb, put this as the last line in the application_setup method:

    StandardAppearance.apply app.window
    

    And then this is my StandardAppearance class:

    class StandardAppearance
      def self.apply(window)
        Dispatch.once do
    
          UINavigationBar.appearance.tap do |o|
            o.setBackgroundImage(UIImage.alloc.init, forBarMetrics: UIBarMetricsDefault)
            o.shadowImage = UIImage.alloc.init
          end
    
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-07 15:09

    I solved this problem with the use of autolayouts. The solution works on different screen sizes and with orientation change.

    extension UINavigationBar {
    
        @IBInspectable var bottomBorderColor: UIColor {
            get {
                return self.bottomBorderColor;
            }
            set {
                let bottomBorderRect = CGRect.zero;
                let bottomBorderView = UIView(frame: bottomBorderRect);
                bottomBorderView.backgroundColor = newValue;
                addSubview(bottomBorderView);
    
                bottomBorderView.translatesAutoresizingMaskIntoConstraints = false;
    
                self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0));
                self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0));
                self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0));
                self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 1));
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 15:14

    Here is another way:

    CALayer *border = [CALayer layer];
    border.borderColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"border"]].CGColor;
    border.borderWidth = 1;
    CALayer *layer = self.navigationController.navigationBar.layer;
    border.frame = CGRectMake(0, layer.bounds.size.height, layer.bounds.size.width, 1);
    [layer addSublayer:border];
    
    0 讨论(0)
  • 2020-12-07 15:16

    I wrote an extension based on the other answers for easier usage in Swift:

    extension UINavigationBar {
    
        func setBottomBorderColor(color: UIColor) {
    
            let navigationSeparator = UIView(frame: CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5))
            navigationSeparator.backgroundColor = color
            navigationSeparator.opaque = true
            navigationSeparator.tag = 123
            if let oldView = self.viewWithTag(123) {
                oldView.removeFromSuperview()
            }
            self.addSubview(navigationSeparator)
    
        }
    }
    

    You can use this extension with calling the method in a context like that:

    self.navigationController?.navigationBar.setBottomBorderColor(UIColor.whiteColor())
    

    I found that pretty useful as I had to deal with that colored-border-problem.

    0 讨论(0)
提交回复
热议问题