OC 添加导航栏item

/**
 * 设定右侧按钮方法
 */
- (void)createMakeBtn{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 40)];
    button.backgroundColor = UIColor.systemGreenColor;
    self.makeBtn = button;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button.titleLabel setFont:[UIFont systemFontOfSize:14 weight:(UIFontWeightRegular)]];
    [button addTarget:self action:@selector(clickMakeBtn) forControlEvents:UIControlEventTouchUpInside];
    button.userInteractionEnabled = NO;
    button.showsTouchWhenHighlighted = NO;
    ///显示的item
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:button];
    ///间距
    UIBarButtonItem *fixItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    self.navigationItem.rightBarButtonItems = @[fixItem, item];
    
    [self updateCreateBtnStatus];
}

/// 更新制作按钮的显示状态
- (void)updateCreateBtnStatus{
    if (self.selectedAssetArr.count > 0) {
        self.makeBtn.backgroundColor = UIColor.blueColor;
        self.makeBtn.userInteractionEnabled = YES;
    }else{
        self.makeBtn.backgroundColor = UIColor.grayColor;
        self.makeBtn.userInteractionEnabled = NO;
    }
    NSString *selectedStr = [NSString stringWithFormat:@"制作(%lu/%d)", (unsigned long)self.selectedAssetArr.count, self.maxSelectedPic];
    [self.makeBtn setTitle:selectedStr forState:(UIControlStateNormal)];
}

  

原文地址:https://www.cnblogs.com/qingzZ/p/14040860.html