iOS 7中UINavigationbar下面的UISegmentedControl

我如何将a UISegmentedControl作为其UINavigationBar下面的一部分?它是连接到UINavigationBar还是一个完整的单独视图,只是作为子视图添加到UINavigationController的视图控制器中。似乎是其中的一部分,UINavigationBar因为酒吧下方有一个阴影。



三国纷争
浏览 769回答 3
3回答

慕的地8271018

这是一个简单的效果。首先,将分段放置在工具栏中。将此工具栏放在导航栏的正下方。设置工具栏视图控制器的代表,并返回UIBarPositionTopAttached在positionForBar:。您可以在商店应用程序中看到,如果执行交互式弹出手势,则分段栏的移动与导航栏的移动不同。那是因为他们不一样。现在删除发际线。“发际线”是UIImageView导航栏的子视图。您可以找到它并将其设置为隐藏。例如,这就是Apple在其本机日历应用程序以及商店应用程序中所做的。请记住在当前视图消失时显示它。如果您稍稍玩一下Apple应用程序,将会看到发际线设置为隐藏在上,viewWillAppear:并显示为viewDidDisappear:。要获得搜索栏的样式,只需将其设置为searchBarStyle即可UISearchBarStyleMinimal。

慕尼黑8549860

现在删除发际线。“细线”是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{&nbsp; &nbsp; [super viewDidLoad];&nbsp; &nbsp; // find the hairline below the navigationBar&nbsp; &nbsp; for (UIView *aView in self.navigationController.navigationBar.subviews) {&nbsp; &nbsp; &nbsp; &nbsp; for (UIView *bView in aView.subviews) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ([bView isKindOfClass:[UIImageView class]] &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bView.bounds.size.width == self.navigationController.navigationBar.frame.size.width &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bView.bounds.size.height < 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.navHairline = (UIImageView *)bView;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}- (void)viewWillAppear:(BOOL)animated{&nbsp; &nbsp; [super viewWillAppear:animated];&nbsp; &nbsp; [self _moveHairline:YES];}- (void)viewWillDisappear:(BOOL)animated{&nbsp; &nbsp; [super viewWillDisappear:animated];&nbsp; &nbsp; [self _moveHairline:NO];}- (void)_moveHairline:(BOOL)appearing{&nbsp; &nbsp; // move the hairline below the segmentbar&nbsp; &nbsp; CGRect hairlineFrame = self.navHairline.frame;&nbsp; &nbsp; if (appearing) {&nbsp; &nbsp; &nbsp; &nbsp; hairlineFrame.origin.y += self.segmentbar.bounds.size.height;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; hairlineFrame.origin.y -= self.segmentbar.bounds.size.height;&nbsp; &nbsp; }&nbsp; &nbsp; self.navHairline.frame = hairlineFrame;}@end我还发现Apple NavBar代码示例(自定义UINavigationBar)对于解决此问题非常有用。另外,请确保处理UIToolbar的顶部边框,它可能会出现,并且您可能会将其与NavBar的细线混淆。我还希望UIToolbar看起来与NavBar完全一样,然后您可能想要调整ToolbarsbarTintColor。

慕桂英546537

这是针对此特定问题的面向协议的Swift方法,基于公认的答案:HideableHairlineViewController.swiftprotocol HideableHairlineViewController {&nbsp; func hideHairline()&nbsp; func showHairline()}extension HideableHairlineViewController where Self: UIViewController {&nbsp; func hideHairline() {&nbsp; &nbsp; findHairline()?.hidden = true&nbsp; }&nbsp; func showHairline() {&nbsp; &nbsp; findHairline()?.hidden = false&nbsp; }&nbsp; private func findHairline() -> UIImageView? {&nbsp; &nbsp; return navigationController?.navigationBar.subviews&nbsp; &nbsp; &nbsp; .flatMap { $0.subviews }&nbsp; &nbsp; &nbsp; .flatMap { $0 as? UIImageView }&nbsp; &nbsp; &nbsp; .filter { $0.bounds.size.width == self.navigationController?.navigationBar.bounds.size.width }&nbsp; &nbsp; &nbsp; .filter { $0.bounds.size.height <= 2 }&nbsp; &nbsp; &nbsp; .first&nbsp; }}SampleViewController.swiftimport UIKitclass SampleViewController: UIViewController, HideableHairlineViewController {&nbsp; @IBOutlet private weak var toolbar: UIToolbar!&nbsp; @IBOutlet private weak var segmentedControl: UISegmentedControl!&nbsp; override func viewWillAppear(animated: Bool) {&nbsp; &nbsp; super.viewWillAppear(animated)&nbsp; &nbsp; hideHairline()&nbsp; }&nbsp; override func viewDidDisappear(animated: Bool) {&nbsp; &nbsp; super.viewDidDisappear(animated)&nbsp; &nbsp; showHairline()&nbsp; }}// MARK: UIToolbarDelegateextension SampleViewController: UIToolbarDelegate {&nbsp; func positionForBar(bar: UIBarPositioning) -> UIBarPosition {&nbsp; &nbsp; return .TopAttached&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS