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,不能加控件。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 怎么在navigationBar加个Left ,这些控件本身只能在固定位置上 先上代码 UIImageView -...
    Roger_单阅读 2,435评论 0 0
  • 如何在navigationBar上加一个left/rightitem (Button),这种item只能有固定的位...
    俊月阅读 2,744评论 0 2
  • UINavigationBar是我们在开发过程中经常要用到的一个控件,下面我会为大家介绍一些常用的用法。 1. 设...
    橙娃阅读 4,012评论 0 1
  • 上午去火车站办点事,回来的时候看到站前广场的LED屏幕上播着厦门电台94.0的广告:陪无聊的人吃饭,不如听有趣的...
    榆树阅读 1,657评论 0 1
  • /1/ 好友西图,是个胖姑娘。 十八岁以前,她说自己很怂。就像糯滋滋一坨肉,大脸短腿小身...
    小灯泡儿阅读 5,208评论 17 50