NavigationBar

怎么在NavigationBar加left,這些控件本身只能在固定位置上

寫入代碼
UIImage* backImage = [UIImage imageNamed:@"nav_order_more"];
    CGRect backframe = CGRectMake(0,0,30,8);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(doClickBackAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem* setRightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.rightBarButtonItem = setRightBarButtonItem;
  • UIImage -> UIButton -> UIBarButtonItem -> self.navigationItem(邏輯)
  • UIBarButtonItem 是一個(gè)類
  • 通過initWithCustomView方法,實(shí)現(xiàn)UIButton -> UIBarButtonItem

怎么在navigationBar加個(gè) Center,這些控件本身只能在固定位置上

 JRSegmentControl *segment = [[JRSegmentControl alloc] initWithFrame:CGRectMake(0, 0, self.itemWidth * self.viewControllers.count, self.itemHeight) titles:self.titles];

    segment.backgroundColor = self.segmentBgColor;
    segment.indicatorViewColor = self.indicatorViewColor;

    segment.delegate = self;
    self.navigationItem.titleView = segment;
  • JRSegmentControl -> self.navigationItem.titleView
  • 通過賦值的方式 self.navigationItem.titleView = segment;

如何在navigationBar在任意位置加控件

UISegmentedControl * contorl = [[UISegmentedControl alloc] initWithItems:@[@"全部",@"待收貨",@"待評(píng)價(jià)"]];
    contorl.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-150/2, 2, 150, 40);
    [contorl addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];
    //默認(rèn)選擇為第一個(gè)
    contorl.selectedSegmentIndex = 0;
    [self.navigationController.navigationBar addSubview:contorl];

UISegmentedControl -> self.navigationController.navigationBar

  • 通過addSubView的方式
  • [self.navigationController.navigationBar addSubview:contorl];

總結(jié):導(dǎo)航欄上只能加UIBarButtonItem,不能直接加控件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容