怎么在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* setLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = setLeftBarButtonItem;
//設置Button的背景圖片钱反,把Button放到BarButtonItem上皇型,通過initWithCustomView的方式加到self.navigationItem.leftBarButtonItem = setLeftBarButtonItem上椰于。
- UIImageView -> UIButton -> UIBarButtonItem -> self.navigationItem
- 通過initWithCustomView方法,實現UIButton -> UIBarButtonItem
怎么在navigationBar
加個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;
//實例化個控件菩佑,設置它的fame(坐標和大小)蓖捶,通過賦值的方式self.navigationItem.titleView,由于titleView繼承的UIView,所以它可以是任何控件沛膳。
如何在navigationBar
在任意位置加空間
UISegmentedControl * contorl = [[UISegmentedControl alloc] initWithItems:@[@"全部",@"待收貨",@"待評價"]];
contorl.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-150/2, 2, 150, 40);
[contorl addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];
//默認選擇為第一個
contorl.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:contorl];
UISegmentedControl -> self.navigationController.navigationBar
通過addSubView的方式
[self.navigationController.navigationBar addSubview:contorl];
//實例化個控件扔枫,設置它的fame(坐標和大小),通過addSubView的方式加到self.navigationController.navigationBar上锹安。
總結:
導航欄上只能加UIBarButton,item
(項目),不可加控件短荐。