移除 UITabbar 上边框线
Remove UITabbar upper border line
我一直在应用程序中使用 UITabbar。
UITabbar 顶部有一条上边界线。
参考下图:-
我用谷歌搜索并尝试了建议的代码:-
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
还有
[[UITabBar appearance] setShadowImage:nil];
self.navigationController.toolbar.clipsToBounds = YES;
但其中 none 个正在运行。有什么解决办法吗?
您只需添加这两行代码即可从 UITabbar 中删除边框:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
// Override point for customization after application launch.
return YES;
}
之前:
之后:
更新:
您也可以设置背景图像并将阴影设置为 nil,如下面的代码
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
输出:
[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];
或者您可以使用
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
或
[[UITabBar appearance] setShadowImage:nil];
tabBar.clipsToBounds = YES;
适合我。
UITabbar
的 shadowImage
属性 负责 UITabbar
上的这条边界线(灰色阴影)。更新此 属性 的值以将其删除。
试试这个,
** Objective-C **
//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];
// or
// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];
** Swift **
//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil
// or
// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()
这是 shadowImage
的苹果指南
@available(iOS 6.0, *)
open var shadowImage: UIImage?
Default is nil. When non-nil, a custom shadow image to show instead of
the default shadow image. For a custom shadow to be shown, a custom
background image must also be set with -setBackgroundImage: (if the
default background image is used, the default shadow image will be
used).
如果不限制范围,我找不到答案。以及使用 UITabBar.appearance().shadowImage
的方式
似乎过时了
所以我做了一个不完美但有效且安全的解决方案来删除iOS10和iOS11没有[=的UITabBar阴影27=] 裁剪到边界。也适用于 iPhone X(如果不适用会很奇怪;))
下面的代码遍历 UITabBarController
层次结构并查找影子视图。 UIImageView
是 _UIBarBackground
的子视图(目前)
extension UITabBarController {
public func hideTopShadow() {
// looking for tabBar
for subview in self.view.subviews {
let tabBarSubviewName = String(describing: type(of: subview))
guard tabBarSubviewName == "UITabBar" else { continue }
// looking for _UIBarBackground. The other subivews are UITabBarButtons
for tabBarSubview in subview.subviews {
let tabBarSubviewName = String(describing: type(of: tabBarSubview))
guard tabBarSubviewName == "_UIBarBackground" else { continue }
// looking for UIImageView. This is the only subview
for shadowView in tabBarSubview.subviews where shadowView is UIImageView {
shadowView.isHidden = true
return
}
}
}
print(" **** ERROR: Could not find the shadow view \(self.self) \(#function)")
}
}
可能的用法。我有一个 UITabBarController 的子类,所以我做了以下操作:
// to avoid excessive runs through the hierarchy after the shadow was hidden
fileprivate var hasHiddenShadow: Bool = false
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard !hasHiddenShadow else { return }
hasHiddenShadow = true
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.hideTopShadow()
}
}
这对我有用 iOS 11,XCode 9.4
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor.white
改进上述答案之一 - 仍然有点乱,但效果更好。上面的答案将隐藏带有自定义图像的 imageView。
for tabBarSubview in self.tabBar.subviews {
let tabBarSubviewName = String(describing: type(of: tabBarSubview))
guard tabBarSubviewName == "_UIBarBackground" else { continue }
tabBarSubview.clipsToBounds = true
}
Swift 5 适合我
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTabBar.clipsToBounds = true
}
iOS 13 & Swift 5:
的工作解决方案
/**
* A custom subclass of `UITabBarController` to use whenever you want
* to hide the upper border of the `UITabBar`.
*/
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.backgroundColor = UIColor.white
// Removing the upper border of the UITabBar.
//
// Note: Don't use `tabBar.clipsToBounds = true` if you want
// to add a custom shadow to the `tabBar`!
//
if #available(iOS 13, *) {
// iOS 13:
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.shadowImage = nil
appearance.shadowColor = nil
tabBar.standardAppearance = appearance
} else {
// iOS 12 and below:
tabBar.shadowImage = UIImage()
tabBar.backgroundImage = UIImage()
}
}
}
更新你的控制器方法
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.clipsToBounds = true
if #available(iOS 13, *) {
self.tabBar.standardAppearance.shadowImage = nil
self.tabBar.standardAppearance.shadowColor = nil
}
}
self.navigationController?.setNavigationBarHidden(true, animated: true)
对我有用
我一直在应用程序中使用 UITabbar。 UITabbar 顶部有一条上边界线。 参考下图:-
我用谷歌搜索并尝试了建议的代码:-
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
还有
[[UITabBar appearance] setShadowImage:nil];
self.navigationController.toolbar.clipsToBounds = YES;
但其中 none 个正在运行。有什么解决办法吗?
您只需添加这两行代码即可从 UITabbar 中删除边框:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
// Override point for customization after application launch.
return YES;
}
之前:
之后:
更新: 您也可以设置背景图像并将阴影设置为 nil,如下面的代码
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
输出:
[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];
或者您可以使用
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
或
[[UITabBar appearance] setShadowImage:nil];
tabBar.clipsToBounds = YES;
适合我。
UITabbar
的 shadowImage
属性 负责 UITabbar
上的这条边界线(灰色阴影)。更新此 属性 的值以将其删除。
试试这个, ** Objective-C **
//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];
// or
// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];
** Swift **
//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil
// or
// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()
这是 shadowImage
@available(iOS 6.0, *)
open var shadowImage: UIImage?
Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage: (if the default background image is used, the default shadow image will be used).
如果不限制范围,我找不到答案。以及使用 UITabBar.appearance().shadowImage
的方式
似乎过时了
所以我做了一个不完美但有效且安全的解决方案来删除iOS10和iOS11没有[=的UITabBar阴影27=] 裁剪到边界。也适用于 iPhone X(如果不适用会很奇怪;))
下面的代码遍历 UITabBarController
层次结构并查找影子视图。 UIImageView
是 _UIBarBackground
extension UITabBarController {
public func hideTopShadow() {
// looking for tabBar
for subview in self.view.subviews {
let tabBarSubviewName = String(describing: type(of: subview))
guard tabBarSubviewName == "UITabBar" else { continue }
// looking for _UIBarBackground. The other subivews are UITabBarButtons
for tabBarSubview in subview.subviews {
let tabBarSubviewName = String(describing: type(of: tabBarSubview))
guard tabBarSubviewName == "_UIBarBackground" else { continue }
// looking for UIImageView. This is the only subview
for shadowView in tabBarSubview.subviews where shadowView is UIImageView {
shadowView.isHidden = true
return
}
}
}
print(" **** ERROR: Could not find the shadow view \(self.self) \(#function)")
}
}
可能的用法。我有一个 UITabBarController 的子类,所以我做了以下操作:
// to avoid excessive runs through the hierarchy after the shadow was hidden
fileprivate var hasHiddenShadow: Bool = false
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard !hasHiddenShadow else { return }
hasHiddenShadow = true
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.hideTopShadow()
}
}
这对我有用 iOS 11,XCode 9.4
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor.white
改进上述答案之一 - 仍然有点乱,但效果更好。上面的答案将隐藏带有自定义图像的 imageView。
for tabBarSubview in self.tabBar.subviews {
let tabBarSubviewName = String(describing: type(of: tabBarSubview))
guard tabBarSubviewName == "_UIBarBackground" else { continue }
tabBarSubview.clipsToBounds = true
}
Swift 5 适合我
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTabBar.clipsToBounds = true
}
iOS 13 & Swift 5:
的工作解决方案/**
* A custom subclass of `UITabBarController` to use whenever you want
* to hide the upper border of the `UITabBar`.
*/
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.backgroundColor = UIColor.white
// Removing the upper border of the UITabBar.
//
// Note: Don't use `tabBar.clipsToBounds = true` if you want
// to add a custom shadow to the `tabBar`!
//
if #available(iOS 13, *) {
// iOS 13:
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.shadowImage = nil
appearance.shadowColor = nil
tabBar.standardAppearance = appearance
} else {
// iOS 12 and below:
tabBar.shadowImage = UIImage()
tabBar.backgroundImage = UIImage()
}
}
}
更新你的控制器方法
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.clipsToBounds = true
if #available(iOS 13, *) {
self.tabBar.standardAppearance.shadowImage = nil
self.tabBar.standardAppearance.shadowColor = nil
}
}
self.navigationController?.setNavigationBarHidden(true, animated: true)
对我有用