iOS设置[self.navigationItem setTitleView:View]

本文介绍如何通过自定义TitleView实现特殊布局需求的NavigationBar,包括添加搜索框等元素,并确保其能够适配不同界面间的导航需求。

自定义NavigationBar

功能需求在NavigationBar上添加搜索框,并对其位置提出了要求,系统中自带的TitleView不能满足,因此查阅了资料,重写了TitleView
自定义的NavigationBar
TitleView.h文件,重写TitleView继承UIView

#import <UIKit/UIKit.h>

@interface TitleView : UIView

@end

TitleView.m文件,重写其父类的Frame

#import "TitleView.h"

@implementation TitleView
- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

- (void)setFrame:(CGRect)frame {

    [super setFrame:CGRectMake(0, 0, self.superview.frame.size.width, self.superview.bounds.size.height)];
}
@end

在需要使用的地方引用

    // 这里之所以要把leftBarButtonItem的title = @“”,设为了防止界面从上一层pushViewController:或是从该界面popViewControllerAnimated:是显示出系统自带的返回箭头
    UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] init];
    [backButtonItem setTitle:@""];
    self.navigationItem.leftBarButtonItem = backButtonItem;

    _titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    _titleView.backgroundColor = [UIColor blackColor];
    self.navigationItem.titleView = _titleView;
分析下面代码的逻辑,但这个页面完成最后设置时,需要解决下面的bug bug信息: Bug 1174015 - 【quick setup】iOS standalone添加ap,quick setup最后一步summary页控件依然显示为Next,安卓为“Confirm”引导更合理 (edit) 基本信息 ► 审核区域▼ Status: ASSIGNED (edit) Importance: major (edit) Bug的类别: UI (edit) 出现频率: 必然 (edit) 能否恢复: --- (edit) 测试拓扑: (edit) 陪测设备: (edit) 重现步骤: iOS standalone添加ap,quick setup到最后一步summary页,观察 (edit) 测试结果: 控件依然显示为Next (edit) 期望结果: 显示为confirm (edit) 问题产品: (edit) 对比测试: Android显示为confirm (edit) 问题分析: 请分析 (edit) 影响评估: 双端不一致 (edit) 代码: // // StandaloneQuickSetupSummaryViewController.m // Omada // // Created by zhuangqiuxiong on 2018/1/8. // Copyright © 2018年 TP-Link. All rights reserved. // #import "StandaloneQuickSetupSummaryViewController.h" #import "VMSLQuickSetupSummary.h" #import "StandaloneQuickSetupApplySettingsViewController.h" #import "TPSLQuickSetupUserAbstractLayer.h" #import "standaloneQuickSetupFlexibleBar.h" @interface StandaloneQuickSetupSummaryViewController ()<UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) VMSLQuickSetupSummary *vmTableViewDataSource; @property (nonatomic, strong) StandaloneQuickSetupFlexibleBar *flexibleBar; @property (nonatomic, strong) UIButton *nextButton; @property (nonatomic, assign) BOOL isForGateway; @end @implementation StandaloneQuickSetupSummaryViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupNavigationItem]; [self buildTableView]; [self buildFlexibleBar]; } - (void)buildTableView { self.tableView = [TPBCommonTableHelper createKeyboardAvoidingTableViewWithDelegate:self andDataSource:nil]; // self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; [self.view addSubview:self.tableView]; // self.tableView.delegate = self; // self.tableView.estimatedRowHeight = 72; // self.tableView.estimatedSectionHeaderHeight = 0; // self.tableView.estimatedSectionFooterHeight = 0; // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // self.tableView.separatorInfo.separatorStyle = TPTableViewSeparatorDetailStyle; // self.tableView.tableFooterView = [[UIView alloc] init]; // self.tableView.backgroundColor = [UIColor tpbBackground]; self.vmTableViewDataSource = [[VMSLQuickSetupSummary alloc] init]; self.tableView.dataSource = self.vmTableViewDataSource; [self.vmTableViewDataSource registerClassInTableView:self.tableView]; UIButton *nextButton = [[UIButton alloc] init]; if (nextButton) { [self.view addSubview:nextButton]; [nextButton setBackgroundColor:[UIColor tpbPrimary]]; nextButton.layer.cornerRadius = 22; [nextButton setTitle:gStandaloneQuickSetup.standaloneQuickSetupSetAccountNavRightButton forState:UIControlStateNormal]; nextButton.titleLabel.font = [UIFont tpr20Regular]; nextButton.titleLabel.textColor = [UIColor tpbTextWhite]; [nextButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; TPWeakSelf; [nextButton mas_makeConstraints:^(MASConstraintMaker *make) { TPStrongSelf; make.leading.mas_equalTo(_self.view).offset(20); make.trailing.mas_equalTo(_self.view).offset(-20); make.height.equalTo(@(44)); make.bottom.equalTo(self.mas_tpSafeAreaLayoutGuide).offset(-8); }]; self.nextButton = nextButton; } TPWeakSelf; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { TPStrongSelf; make.top.leading.trailing.equalTo(self.mas_tpSafeAreaLayoutGuide); make.bottom.equalTo(self.nextButton.mas_top).offset(-2); }]; } - (void)buildFlexibleBar { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; CGFloat minBarHeight = kFlexibleBar_MinHeight; self.flexibleBar = [[StandaloneQuickSetupFlexibleBar alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, maxBarHeight) andMaximumBarHeight:maxBarHeight andMinimumBarHeight:minBarHeight andElasticMaximumHeightAtTop:NO andViewController:self andScrollView:self.tableView]; if ( self.flexibleBar ) { [self.view addSubview:self.flexibleBar]; self.flexibleBar.titleString = gStandaloneQuickSetup.standaloneQuickSetupSummaryNavTitle; [self.flexibleBar setupSubViewsAndLayoutAttributes]; // self.flexibleBar.backgroundColor = [UIColor tpbBackground]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tpbPrivacyProtectionEnabled = YES; [self.vmTableViewDataSource reloadData]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - TableView Delegate - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // return 40; return UITableViewAutomaticDimension; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [self.vmTableViewDataSource viewForSectionHeaderInSection:section]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { // return 0.01; return TPBDesign.list.sectionFooterHeight; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return [UIView new]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark - 导航栏相关 - (void)setupNavigationItem { // [self setNavigationBarTitle:gStandaloneQuickSetup.standaloneQuickSetupSummaryNavTitle]; // [self.tpNavigationController setNavigationBarTitleFont:[UIFont tpr20Regular] andColor:[UIColor tpbTextPrimary]]; self.isNavigationBarTransparent = YES; UIBarButtonItem *leftBarButtonItem = [self createBarButtonItemItemWithStyle:TPViewControllerBarButtonItemStyleBackInBlue andTarget:self andAction:@selector(back)]; self.navigationItem.leftBarButtonItem = leftBarButtonItem; } - (void)back { [self.tpNavigationController popViewControllerAnimated:YES]; } - (void)next { TPWeakSelf; [self showWaitingToastView]; [[[ALStandaloneDeviceContext currentInstance] checkLoginStatus] addCompletionOnMainThread:^(TPHandleResult *result) { TPStrongSelf; [_self dismissToastViewWithCompleteBlock:^{ if (result.success) { if (self.isForGateway) { // 跳转到Gateway页面 [_self.tpNavigationController pushViewControllerWithCustomAnimation:[[StandaloneQuickSetupApplySettingsViewController alloc] initForGateway:self.isForGateway] animated:YES]; } else { // 跳转到EAP页面 [_self.tpNavigationController pushViewControllerWithCustomAnimation:[[StandaloneQuickSetupApplySettingsViewController alloc] init] animated:YES]; } } else { DDLogInfo(@"检查QuickSetup前置条件不满足"); [self showCommonFailedToastView]; } }]; }]; } - (void)orientationChangeAction { //横屏模式下 if (self.orientation == TPBInterfaceOrientationRight || self.orientation == TPBInterfaceOrientationLeft) { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; self.flexibleBar.frame = CGRectMake(0, 0, MAX(kScreen_Width, kScreen_Height), maxBarHeight); } //竖屏模式下 else { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; self.flexibleBar.frame = CGRectMake(0, 0, MIN(kScreen_Width, kScreen_Height), maxBarHeight); } } // MARK: 工具函数 - (BOOL)isForGateway { return [TPStandaloneClient currentInstance].discoveredDevice.isGateway; } @end
最新发布
12-06
继续判断分层 internal func addRightBarButtonItem() { let addImage = TPImageLiteral("common_add_unlogin") let addButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: addImage.size.width, height: addImage.size.height)) addButton.setImage(addImage, for: .normal) addButton.addTarget(self, action: #selector(addDeviceButtonClicked(_:)), for: .touchUpInside) addButton.tintColor = .tpbPrimary addButtonmItem = UIBarButtonItem.init(customView: addButton) self.addButton = addButton negativeButtonItem = UIBarButtonItem.init(customView: UIView(frame: CGRectZero)) let changeDisplayImage = TPImageLiteral("deviceList_display_bigPic") changeDisplayButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: changeDisplayImage.size.width, height: changeDisplayImage.size.height)) changeDisplayButton.setImage(changeDisplayImage, for: .normal) changeDisplayButton.addTarget(self, action: #selector(toggleDisplayMode), for: .touchUpInside) changeDisplayButton.tintColor = .tpbPrimary changeDisplayTypeItem = UIBarButtonItem.init(customView: changeDisplayButton); let searchImage = TPImageLiteral("common_search") let searchButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height)) searchButton.setImage(searchImage, for: .normal) searchButton.addTarget(self, action: #selector(unbindNavigateToSearch), for: .touchUpInside) searchButton.tintColor = .tpbPrimary searchItem = UIBarButtonItem.init(customView: searchButton) self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem] }
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值