UISegmentedControl在iOS 7中的UINavigationbar下面

yoe*_*ven 96 uinavigationbar uisegmentedcontrol ios

如何将其UISegmentedControl作为UINavigationBar下方的一部分呢?它是否连接到UINavigationBar或者它是一个完整的单独视图,只是作为子视图添加到UINavigationController视图控制器中.看起来它是UINavigationBar因为酒吧下面有阴影所以是它的一部分.

在此输入图像描述

Leo*_*ica 150

这是一个简单的效果.

首先,将一个段放在工具栏中.将此工具栏放在导航栏的正下方.设置工具栏视图控制器的代表,并返回UIBarPositionTopAttached在positionForBar:.您可以在商店应用中看到,如果您执行交互式弹出手势,则段栏的移动方式与导航栏不同.那是因为他们不是同一个酒吧.

在此输入图像描述

现在删除发际线."发际线"是UIImageView导航栏的子视图.您可以找到它并将其设置为隐藏.这就是Apple在其原生日历应用程序中所做的事情,以及商店应用程序.记得在当前视图消失时显示它.如果您使用Apple应用程序稍微玩一下,您会看到发际线设置为隐藏viewWillAppear:并设置为显示viewDidDisappear:.

要实现搜索栏的样式,只需将栏设置searchBarStyle为UISearchBarStyleMinimal.

  • 好答案!不要忘记更新tableView的contentInset以避免toolBar覆盖内容:-) (6认同)
  • 仍然混淆了添加工具栏的最佳方法,我应该在`initWithRect:CGRectMake(0,self.toplayoutGuide.length,320,44)`中硬编码帧,还是使用autolayout来定位它?什么是childViews的新顶部,那将是`self.toplayoutGuide.length + 44`? (2认同)
  • @Vrutin 不要使用栏按钮项目。相反,添加为工具栏的子视图。然后您可以将大小设置为工具栏的大小。 (2认同)

Sim*_*mon 14

现在删除发际线."发际线"是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
Run Code Online (Sandbox Code Playgroud)

我还发现Apple NavBar代码示例(自定义UINavigationBar)对解决此问题非常有帮助.

另外一定要处理UIToolbar的顶部边框,它可能会出现,你可能会把它与NavBar的发际线混淆.我还希望UIToolbar看起来与NavBar完全一样,你可能想要调整工具栏barTintColor.


Iva*_*uel 13

这是针对这个特定问题的一种面向协议的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
  }

}
Run Code Online (Sandbox Code Playgroud)

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
  }

}
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以在Apple示例代码中找到带有UISegmentedControl的导航栏:https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html

或者您可以通过编程方式创建它,这是我在其他线程中的答案中的代码 将分段控件添加到导航栏并使用按钮保持标题


iTS*_*gar 7

我想做同样的事情..并得到了这个:


1 - UINavigationBar子类

//-------------------------
// UINavigationBarCustom.h
//-------------------------
#import <UIKit/UIKit.h>

@interface UINavigationBarCustom : UINavigationBar

@end


//-------------------------
// UINavigationBarCustom.m
//-------------------------
#import "UINavigationBarCustom.h"

const CGFloat MyNavigationBarHeightIncrease = 38.f;

@implementation UINavigationBarCustom


- (id)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];

    if (self) {
        [self initialize];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {
        [self initialize];
    }

    return self;
}

- (void)initialize {
    // Set tittle position for top
    [self setTitleVerticalPositionAdjustment:-(MyNavigationBarHeightIncrease) forBarMetrics:UIBarMetricsDefault];
}

- (CGSize)sizeThatFits:(CGSize)size {
    // Increase NavBar size
    CGSize amendedSize = [super sizeThatFits:size];
    amendedSize.height += MyNavigationBarHeightIncrease;

    return amendedSize;
}

- (void)layoutSubviews {
// Set buttons position for top
    [super layoutSubviews];

    NSArray *classNamesToReposition = @[@"UINavigationButton"];

    for (UIView *view in [self subviews]) {

        if ([classNamesToReposition containsObject:NSStringFromClass([view class])]) {

            CGRect frame = [view frame];
            frame.origin.y -= MyNavigationBarHeightIncrease;

            [view setFrame:frame];
        }
    }
}

- (void)didAddSubview:(UIView *)subview
{
    // Set segmented position
    [super didAddSubview:subview];

    if ([subview isKindOfClass:[UISegmentedControl class]])
    {
        CGRect frame = subview.frame;
        frame.origin.y += MyNavigationBarHeightIncrease;
        subview.frame = frame;
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

2 - 使用子类设置NavigationController

使用子类设置NavigationController


3 - 在navigationBar中添加UISegmentedControl

在此输入图像描述


4 - 运行和乐趣 - >不要忘记在两者上涂上相同的颜色

在此输入图像描述


搜索来源:

黑客UINavigationBar

所以问题