Colors iOS7-更改UINavigationBar边框颜色
是否可以在iOS7中更改UINavigationBar的灰色边框底色 我已尝试删除到边界,但这不起作用:Colors iOS7-更改UINavigationBar边框颜色,colors,border,uinavigationbar,ios7,xcode5,Colors,Border,Uinavigationbar,Ios7,Xcode5,是否可以在iOS7中更改UINavigationBar的灰色边框底色 我已尝试删除到边界,但这不起作用: [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; 谢谢 要删除阴影而不是边框,需要执行以下操作: [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefa
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
谢谢 要删除阴影而不是边框,需要执行以下操作:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
要更改边框,请使用2像素宽的图像:
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"2pxWidthLineImage"]];
这将帮助您:)
还有一种方法:
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];
以下是随高度更改底色的类别:
[self.navigationController.navigationBar setBottomBorderColor:[UIColor redColor] height:1];
目标C:
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)
}
}
UINavigationBar+Helper.h
#import <UIKit/UIKit.h>
@interface UINavigationBar (Helper)
- (void)setBottomBorderColor:(UIColor *)color height:(CGFloat)height;
@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)
}
}
我将RubyMotion与RedPotion gem一起使用,其中包括一个
标准外观类。这就是我所做的
将这一行放在app\u delegate.rb
的顶部,就在on\u load
方法之前:
ApplicationStylesheet.new(nil).application_setup
StandardAppearance.apply app.window
然后,在application\u stylesheet.rb
中,将其作为application\u setup
方法的最后一行:
ApplicationStylesheet.new(nil).application_setup
StandardAppearance.apply app.window
然后这是我的标准外观
课程:
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
如果您像我一样喜欢简单而粗糙的解决方案,请创建一个覆盖默认边框的视图:
UIView *navBarLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.navigationController.navigationBar.frame),
CGRectGetWidth(self.navigationController.navigationBar.frame), 1)];
navBarLineView.backgroundColor = [UIColor redColor];
[self.navigationController.navigationBar addSubview:navBarLineView];
好吧,若你们想移除底部边框,你们可以将阴影图像设置为空图像
[navigationBar setShadowImage:[UIImage new]];
所以,如果您想将其设置为另一种颜色,只需使用该颜色创建图像,我使用一个辅助函数从下面的颜色创建图像(原始源)
在我的导航栏里
[navigationBar setShadowImage:[UIImage imageFromColor:[UIColor redColor] forSize:CGSizeMake(CGRectGetWidth(self.tableView.frame), 1)]];
就是这样,对我有用,希望这能帮上忙。请考虑更改已接受的答案,因为它不起作用,可能会混淆 < P>我发现改变颜色的唯一方式是:
override func viewDidLoad() {
super.viewDidLoad()
if let navigationController = self.navigationController {
let navigationBar = navigationController.navigationBar
let navigationSeparator = UIView(frame: CGRectMake(0, navigationBar.frame.size.height - 1, navigationBar.frame.size.width, 0.5))
navigationSeparator.backgroundColor = UIColor.redColor() // Here your custom color
navigationSeparator.opaque = true
self.navigationController?.navigationBar.addSubview(navigationSeparator)
}
}
budidino解决方案效果非常好。这是给斯威夫特的:
let navBarLineView = UIView(frame: CGRectMake(0,
CGRectGetHeight((navigationController?.navigationBar.frame)!),
CGRectGetWidth((self.navigationController?.navigationBar.frame)!),
1))
navBarLineView.backgroundColor = UIColor.whiteColor()
navigationController?.navigationBar.addSubview(navBarLineView)
为了便于在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)
}
}
您可以使用此扩展在如下上下文中调用该方法:
self.navigationController?.navigationBar.setBottomBorderColor(UIColor.whiteColor())
我发现这非常有用,因为我必须处理彩色边界问题。根据@sash的回答,我使用自动布局在Swift中进行了扩展,解释道
本质上,其他解决方案存在以下缺陷:
- 如果使用UIImage解决方案,则无法添加dropshadow
- 添加的子视图在视图旋转时不会调整大小
扩展UINavigationBar{
func setBottomBorderColor(颜色:UIColor,高度:CGFloat)->UIView{
让bottomBorderView=UIView(帧:CGRectZero)
bottomBorderView.translatesAutoresizingMaskIntoConstraints=false
bottomBorderView.backgroundColor=颜色
self.addSubview(bottomBorderView)
let view=[“border”:bottomBorderView]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“H:|[border]|”,选项:[],度量:nil,视图:视图))
self.addConstraint(NSLayoutConstraint(项:bottomBorderView,属性:.Height,relatedBy:.Equal,toItem:nil,属性:.NotaAttribute,乘数:1.0,常数:Height))
self.addConstraint(NSLayoutConstraint(项:bottomBorderView,属性:.Bottom,relatedBy:.Equal,toItem:self,属性:.Bottom,乘数:1.0,常量:高度))
返回底部边界视图
}
}
这让你仍然可以添加阴影,如果你需要的话,这很好地处理旋转 以下是创建清晰颜色图像的方法:
+ (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;
}
以下是消除烦人底线的用法:
navigationBar.shadowImage = [UIImage imageFromColor:[UIColor clearColor] withSize:CGSizeMake(1.0f, 1.0f)];
要基于@sash的Swift实现,您可以通过使用约束使边界对旋转/特征更改做出响应:
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])
}
}
可以使用“显示”查看边框颜色是UIImageView的背景色。
因此,直接修改imageView的背景色或将其隐藏
代码:我在@interface qdtabbarcontroller:UITabBarController中编写
Class backGroundClass = NSClassFromString(@"_UIBarBackground");
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:backGroundClass]) {
for (UIView *view2 in view.subviews) {
if ([view2 isKindOfClass:[UIImageView class]]) {
dispatch_async(dispatch_get_main_queue(), ^{
view2.backgroundColor = [UIColor redColor];
});
};
};
break;
}
}
我使用自动布局解决了这个问题。该解决方案适用于不同的屏幕尺寸和方向变化
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));
}
}
}
如何添加边框?(更改颜色)此方法将删除导航栏的半透明特性。setShadowImage
似乎不再存在。这是整个导航栏周围的边框,而不是作者试图更改的边框。当试图设置底部边界时,它没有帮助。为什么这会被否决?有人能解释一下吗?!我试图用RubyMoon解决这个问题,这就是我的解决方案。我想把它放在这里当然没什么坏处。我不知道你的回答对这里的海报有什么帮助。海报甚至没有使用RubyMoment和/或RedPotion。提交一个不相关的答案可能会误导正在寻找特定问题解决方案的读者。是的,这肯定会影响提交的质量。即使你的答案是正确的,海报仅仅为了改变边框颜色而从Objective-C切换到RubyMoon的可能性有多大application\u stylesheet.rb
在iOS/Objective-C/Swift中甚至都不是一个东西。@denniss我不建议有人为了一个修复而切换到RubyMotion。那根本不可能。但在iOS开发方面,有一些不同的方法:你可以直接使用Xcode,你可以使用Swift,你可以使用RubyMoon,等等。实际上有一个解决方案,有人提到如何使用Swift来完成。你也投票否决了他们吗?我只是在想,如果有人在搜索“Change-UINavigationBar-border-rubymotion”,那么他们可能会找到我的解决方案。另外,我也被其他问题的类似帖子所帮助。你可以使用JavaScript、C、C++、Stand……还有这个框架让你可以用java来构建IOS…您认为使用这些语言/框架的提交在这里是可以接受的吗?不,我不会投票反对使用Swift提交,因为它是官方支持的iOS开发语言之一,而且将Swift转换为Obj-C相当容易(反之亦然)。我是说,看看苹果的文档。。。。您可以选择使用Swift或Obj-C查看代码示例。我不认为Ruby是选项之一。@denniss Ok。很公平
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));
}
}
}