navigationItem左右按钮偏移的问题(left,right)

本文介绍如何在iOS应用中使用Objective-C自定义导航栏左侧按钮,包括设置按钮图像、响应点击事件及添加关闭按钮。

- (void)addBarButtonItemWithImageNameleftCustom:(NSString *)imageName selector:(SEL)selector close:(SEL)closeBtn

{

    UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [barButton setFrame:CGRectMake(0, 0, 30, 30)];

    [barButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

    [barButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:barButton];

    

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]

                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                       target:nil action:nil];

    negativeSpacer.width = -13;

    

    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];

    [close setFrame:CGRectMake(0, 0, 30, 30)];

    [close setTitle:@"关闭" forState:UIControlStateNormal];

    close.titleLabel.font = [UIFont boldSystemFontOfSize:14];

    [close setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [close addTarget:self action:closeBtn forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithCustomView:close];


    

    self.navigationItem.leftBarButtonItems = @[negativeSpacer,item,closeItem];

    

}

private func configureTabsAndUIForLoginType() { if !isDeviceListOld { tabNames = appContext.isVmsLogin ? [ LocalizedString(key: deviceListAllDevices)] : [LocalizedString(key: deviceCollectFavorites), LocalizedString(key: deviceListAllDevices)] } if appContext.isVmsLogin { if !showLocalDeviceOnly { if !isDeviceListOld { //支持收藏,allDeviceTabIndex、siteDeviceTabIndex可以设置为1、2 if !DeviceListMasterViewController.isSupportVMSFavorite { //不支持收藏 collectDviceTabIndex = -1 allDeviceTabIndex = 0 siteDeviceTabIndex = 1 } else { collectDviceTabIndex = 0 allDeviceTabIndex = 1 siteDeviceTabIndex = 2 } tabBarContainerTopConstraint.constant = 10 moreSiteButton.setImage(TPImageLiteral("devicelist_menu_more"), for: .normal) moreSiteButton.addTarget(self, action: #selector(menuButtonClicked(_:)), for: .touchUpInside) moreSiteButton.backgroundColor = .clear tabCollectionHeightConstant.constant = 30 tabNames.removeAll() if DeviceListMasterViewController.isSupportVMSFavorite { tabNames.append(LocalizedString(key: deviceCollectFavorites)) } tabNames.append(LocalizedString(key: deviceListAllDevices)) selectedTabType = UserDefaults.standard.deviceListSelectedType ?? .all if DeviceListMasterViewController.isSupportVMSFavorite { selectedTabIndex = selectedTabType.rawValue } selectedSiteInfo = DeviceListMasterViewController.getSavedSelectedSiteInfo() if selectedSiteInfo != nil && selectedSiteInfo?.siteId != 0 && selectedSiteInfo?.siteName != "" { tabNames.append(selectedSiteInfo?.siteName ?? "") if selectedTabType == .site { selectedTabIndex = siteDeviceTabIndex } } else { tabNames.append(LocalizedString(key: commonChoose)) } } else { addCustomNavigationLeftBarButtonItem(title: nil, image: TPImageLiteral("common_light_back_nor")) //新版本vms才支持收藏,allDeviceTabIndex、siteDeviceTabIndex设置为以前的0、1 collectDviceTabIndex = -1 allDeviceTabIndex = 0 siteDeviceTabIndex = 1 tabCollectionHeightConstant.constant = 52 moreSiteButton.setImage(TPImageLiteral("vms_site_more"), for: .normal) moreSiteButton.addTarget(self, action: #selector(moreSiteButtonClick), for: .touchUpInside) moreSiteButton.backgroundColor = .clear self.titleLabel.text = currentSiteInfo?.siteName ?? "" let titleView = UIView() titleView.addSubview(self.titleLabel) NSLayoutConstraint.init(item: self.titleLabel, attribute: .centerY, relatedBy: .equal, toItem: titleView, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint.init(item: self.titleLabel, attribute: .leading, relatedBy: .equal, toItem: titleView, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint.init(item: self.titleLabel, attribute: .trailing, relatedBy: .equal, toItem: titleView, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true self.progressView = ProgressView.init(frame: CGRect.init(x: 0, y: 0, width: 18, height: 18), style: .circleProgressView) self.progressView?.translatesAutoresizingMaskIntoConstraints = false self.progressView?.isHidden = true if let loadingEventListView = self.progressView { titleView.addSubview(loadingEventListView) NSLayoutConstraint.init(item: loadingEventListView, attribute: .centerY, relatedBy: .equal, toItem: titleView, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint.init(item: loadingEventListView, attribute: .trailing, relatedBy: .equal, toItem: self.titleLabel, attribute: .leading, multiplier: 1.0, constant: -11).isActive = true NSLayoutConstraint.init(item: loadingEventListView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 18).isActive = true NSLayoutConstraint.init(item: loadingEventListView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 18).isActive = true } self.navigationItem.titleView = titleView } } else { addCustomNavigationLeftBarButtonItem(title: nil, image: TPImageLiteral("common_light_back_nor")) moreSiteButton.setImage(TPImageLiteral("media_multi_style_more"), for: .normal) moreSiteButton.addTarget(self, action: #selector(deviceListMoreButtonClickForPerson), for: .touchUpInside) moreSiteButton.backgroundColor = .clear } } else { // navigationItem.leftBarButtonItem = nil tabCollectionHeightConstant.constant = 30 if showLocalDeviceOnly { tabCollectionHeightConstant.constant = 52 tabNames.removeAll() tabNames.append(LocalizedString(key: deviceListSubTitleDeviceList)) addCustomNavigationLeftBarButtonItem(title: nil, image: TPImageLiteral("common_light_back_nor")) } selectedTabTypeForPersonal = UserDefaults.standard.deviceListSelectedTypeForPersonal ?? .all if selectedTabTypeForPersonal == .favorite { selectedTabIndex = collectDviceTabIndexForPersonal } else { selectedTabIndex = allDviceTabIndexForPersonal } moreSiteButton.setImage(TPImageLiteral("common_light_more_nor"), for: .normal) moreSiteButton.addTarget(self, action: #selector(deviceListMoreButtonClickForPerson), for: .touchUpInside) moreSiteButton.setEnlargeEdgeWithTop(10.0, left: 20.0, bottom: 20.0, right: 20.0) moreSiteButton.backgroundColor = .clear } if showLocalDeviceOnly { moreSiteButton.isHidden = localDeviceCount == 0 } else { moreSiteButton.isHidden = !appContext.isLogin } } 把这个里面的逻辑可以分个大类,现在有点太长了,然后在viewdidload中也对应修改一下
最新发布
11-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值