ナビゲーションバーの下の境界をどのように変更できるのか教えてもらえますか?ナビゲーションバーの下にある罫線の色を変更するにはどうすればいいですか?
Iはより柔らかい色に現在のブラックライトからそれを変更したいです。 ここにお手伝いします
ナビゲーションバーの下の境界をどのように変更できるのか教えてもらえますか?ナビゲーションバーの下にある罫線の色を変更するにはどうすればいいですか?
Iはより柔らかい色に現在のブラックライトからそれを変更したいです。 ここにお手伝いします
UINavigationBar
のtintColor
プロパティ以外のナビゲーションカラーの境界線の色を変更する方法はありません。
私はそのボーダーサイズのUIViewを作成し、ナビゲーションバーの下に配置するか、subView
として追加することをお勧めします。また、あなたのナビゲーションバーに
を子ビューを追加することができます
UIView *navBorder = [[UIView alloc] initWithFrame:CGRectMake(0,navigationBar.frame.size.height-1,navigationBar.frame.size.width, 1)];
// Change the frame size to suit yours //
[navBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:0.8f]];
[navBorder setOpaque:YES];
[navigationBar addSubview:navBorder];
[navBorder release];
次のコードは、navigationBarにもかかわらず、あなたのナビゲーションバー
UINavigationBar* navBar = self.navigationController.navigationBar;
int borderSize = 4;
UIView *navBorder = [[UIView alloc] initWithFrame:CGRectMake(0,navBar.frame.size.height-borderSize,navBar.frame.size.width, borderSize)];
[navBorder setBackgroundColor:[UIColor blueColor]];
[self.navigationController.navigationBar addSubview:navBorder];
以下の4ピクセル深い青色の境界線が読み取り専用の追加されますUINavigationControllerのプロパティでは、 "setValue:forKey:"によって この制限が回避できます。このメソッドは、AppStoreに正常に提出された5つのアプリケーションで承認されました。
UINavigationBarをサブクラス化し、drawRect:メソッドを必要に応じて変更できます。 例えば、
@implementation CustomNavigationBar
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
[backgroundImage drawInRect:rect];
}
あなたはUINavigationControllerをサブクラス化することができた後とinitWithRootViewControllerを変更:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
{
CustomNavigationBar *navBar = [CustomNavigationBar new];
[self setValue:navBar forKey:@"navigationBar"];
}
return self;
}
をまたあなたはUINavigationControllerのためのカテゴリーを作り、initWithRootViewControllerための方法のスウィズリングを実装することにより、このアプローチを変えることができます。
P.S.を昨日、私の新しいアプリはAppStoreにこのアプローチで何の問題もなく現れました。
iOSの7のためには、これを使用することができます:スウィフトで
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
を@Legolasの答えのように:
if let navigationController = self.navigationController {
let navigationBar = navigationController.navigationBar
let navBorder: UIView = UIView(frame: CGRectMake(0, navigationBar.frame.size.height - 1, navigationBar.frame.size.width, 1))
// Set the color you want here
navBorder.backgroundColor = UIColor(red: 0.19, green: 0.19, blue: 0.2, alpha: 1)
navBorder.opaque = true
self.navigationController?.navigationBar.addSubview(navBorder)
}
はあなたのUIViewController
のviewDidLoad()
に入れることができます。
ビューフレームの原点の 'y'は、上記の' navigationBar.frame.size.height-1'の代わりに 'navigationBar.frame.size.height'でなければなりません。 – Zhao
私は、以前の答えと問題のカップルが見つかりました:あなたはサブビューを追加する場合
一つの方法は、自動レイアウトを使用することです:
extension UINavigationBar {
func setBottomBorderColor(color: UIColor, height: CGFloat) -> UIView {
let bottomBorderView = UIView(frame: CGRectZero)
bottomBorderView.translatesAutoresizingMaskIntoConstraints = false
bottomBorderView.backgroundColor = color
self.addSubview(bottomBorderView)
let views = ["border": bottomBorderView]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[border]|", options: [], metrics: nil, views: views))
self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height))
self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: height))
return bottomBorderView
}
}
私は国境を返す理由は、回転中にあなたがnavigation bar
の途中でそれを見ないということですので、私はそれを隠します回転中に。
素晴らしい!私のためにこれらの作品:)ありがとう! – Zhen