3 回答
TA贡献1796条经验 获得超4个赞
这是一个简单的效果。
首先,将分段放置在工具栏中。将此工具栏放在导航栏的正下方。设置工具栏视图控制器的代表,并返回UIBarPositionTopAttached
在positionForBar:
。您可以在商店应用程序中看到,如果执行交互式弹出手势,则分段栏的移动与导航栏的移动不同。那是因为他们不一样。
现在删除发际线。“发际线”是UIImageView
导航栏的子视图。您可以找到它并将其设置为隐藏。例如,这就是Apple在其本机日历应用程序以及商店应用程序中所做的。请记住在当前视图消失时显示它。如果您稍稍玩一下Apple应用程序,将会看到发际线设置为隐藏在上,viewWillAppear:
并显示为viewDidDisappear:
。
要获得搜索栏的样式,只需将其设置为searchBarStyle
即可UISearchBarStyleMinimal
。
TA贡献1818条经验 获得超11个赞
现在删除发际线。“细线”是UIImageView,它是导航栏的子视图。您可以找到它并将其设置为隐藏。例如,这就是Apple在其本机日历应用程序以及商店应用程序中所做的。请记住在当前视图消失时显示它。如果您稍微玩一下Apple应用程序,将会看到发际线在viewWillAppear:上设置为隐藏,并在viewDidDisappear:上显示。
另一种方法是查找发际线并将其移至添加的工具栏下方。这是我想出的。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *segmentbar;
@property (weak, nonatomic) UIImageView *navHairline;
@end
@implementation ViewController
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// find the hairline below the navigationBar
for (UIView *aView in self.navigationController.navigationBar.subviews) {
for (UIView *bView in aView.subviews) {
if ([bView isKindOfClass:[UIImageView class]] &&
bView.bounds.size.width == self.navigationController.navigationBar.frame.size.width &&
bView.bounds.size.height < 2) {
self.navHairline = (UIImageView *)bView;
}
}
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self _moveHairline:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self _moveHairline:NO];
}
- (void)_moveHairline:(BOOL)appearing
{
// move the hairline below the segmentbar
CGRect hairlineFrame = self.navHairline.frame;
if (appearing) {
hairlineFrame.origin.y += self.segmentbar.bounds.size.height;
} else {
hairlineFrame.origin.y -= self.segmentbar.bounds.size.height;
}
self.navHairline.frame = hairlineFrame;
}
@end
我还发现Apple NavBar代码示例(自定义UINavigationBar)对于解决此问题非常有用。
另外,请确保处理UIToolbar的顶部边框,它可能会出现,并且您可能会将其与NavBar的细线混淆。我还希望UIToolbar看起来与NavBar完全一样,然后您可能想要调整ToolbarsbarTintColor。
TA贡献1848条经验 获得超10个赞
这是针对此特定问题的面向协议的Swift方法,基于公认的答案:
HideableHairlineViewController.swift
protocol HideableHairlineViewController {
func hideHairline()
func showHairline()
}
extension HideableHairlineViewController where Self: UIViewController {
func hideHairline() {
findHairline()?.hidden = true
}
func showHairline() {
findHairline()?.hidden = false
}
private func findHairline() -> UIImageView? {
return navigationController?.navigationBar.subviews
.flatMap { $0.subviews }
.flatMap { $0 as? UIImageView }
.filter { $0.bounds.size.width == self.navigationController?.navigationBar.bounds.size.width }
.filter { $0.bounds.size.height <= 2 }
.first
}
}
SampleViewController.swift
import UIKit
class SampleViewController: UIViewController, HideableHairlineViewController {
@IBOutlet private weak var toolbar: UIToolbar!
@IBOutlet private weak var segmentedControl: UISegmentedControl!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
hideHairline()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
showHairline()
}
}
// MARK: UIToolbarDelegate
extension SampleViewController: UIToolbarDelegate {
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return .TopAttached
}
}
- 3 回答
- 0 关注
- 812 浏览
添加回答
举报