IOS NavigationBar相关

怎么在navigationBar加个Left,这些控件本身只能在固定位置上?

先上代码
//加载图片名称
    UIImage* moreImage = [UIImage imageNamed:@"more"];
    //设置坐标大小
    CGRect moreframe = CGRectMake(0,0,40,10);
    //将Button附到frame
    UIButton* moreButton= [[UIButton alloc] initWithFrame:moreframe];
    [moreButton setBackgroundImage:moreImage forState:UIControlStateNormal];
    //title的字号为13
    moreButton.titleLabel.font=[UIFont systemFontOfSize:13];
    //点击事件
    [moreButton addTarget:self action:@selector(doClickBackAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem* setRightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
    self.navigationItem.rightBarButtonItem = setRightBarButtonItem;
    
  • UIImage -> UIButton -> UIBarButtonItem -> self.navigationItem
    通过设置UIbutton的属性UIImage,使用initwithcustomview方法,将UIbutton放到UIBarButtonITem上。

  • 通过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
    因为Titleview继承UIView,所以所有继承UIView的控件都可以添加到TitleView上。
  • 通过赋值的方式
self.navigationItem.titleView = segment;

如何在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的方式
    或者通过addTarget 。。。action。。。forControlEvents方法。
[self.navigationController.navigationBar addSubview:contorl];

总结:

  • 导航栏

  • 只能加UIBarButton,Item,不能加控件。

你可能感兴趣的:(IOS NavigationBar相关)