美文网首页
导航条设置

导航条设置

作者: huicuihui | 来源:发表于2018-02-23 11:08 被阅读24次

    设置导航条title

    self.navigationItem.title = @"设置title";
    

    设置导航条title样式

        NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:19],NSFontAttributeName, nil];
        [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    

    设置导航条背景图片

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航条背景图片"] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航条背景图片"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    

    导航条样式

    下面记录几种导航条按钮的创建方法:

    1、系统提供的设置文字或者图片

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"设置文字" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"图片名字"] style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    

    2、修改系统导航条按钮内容样式
    UIBarButtonItemStyleDone文字颜色会加粗,UIBarButtonItemStylePlain文字颜色不加粗

        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"右侧按钮" style:UIBarButtonItemStyleDone target:self action:@selector(right:)];
        //设置文字的字体颜色
        right.tintColor = [UIColor blackColor];
        self.navigationItem.rightBarButtonItem = right;
    

    3、自定义一个视图控件(按钮)

        //右侧的菜单按钮
        UIButton *menuBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        [menuBtn addTarget:self action:@selector(showDropDownMenu) forControlEvents:UIControlEventTouchUpInside];
        [menuBtn setImage:[UIImage imageNamed:@"nemuItem"] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    

    设置按钮文字字体颜色和大小

        UIButton * button_back = [UIButton buttonWithType:UIButtonTypeCustom];
        [button_back setTitle:@"右侧按钮" forState:UIControlStateNormal];
        [button_back setTitleColor:UIColorFromRGB(0x1c257) forState:UIControlStateNormal];
        button_back.titleLabel.font = [UIFont systemFontOfSize:16];
        [button_back addTarget:self action:@selector(right:) forControlEvents:UIControlEventTouchUpInside];
    
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button_back];
        self.navigationItem.rightBarButtonItem = backButton;
    

    4、titleView

        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]];
        segmentedControl.frame = CGRectMake(0, 0, 130, 30);
        segmentedControl.selectedSegmentIndex = 0;
        segmentedControl.tintColor = [UIColor whiteColor]; //设置选中状态的背景颜色
        self.navigationItem.titleView = segmentedControl;
    

    相关文章

      网友评论

          本文标题:导航条设置

          本文链接:https://www.haomeiwen.com/subject/jxatxftx.html