码迷,mamicode.com
首页 > 移动开发 > 详细

ios 导航栏(自定义和使用系统方式)

时间:2014-11-17 17:51:26      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:blog   io   color   ar   os   使用   for   div   on   

系统方式:

    //1.设置导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc]init];
    
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.backgroundColor = [[UIColor alloc] initWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
    
    //2.导航面板左边的取消按钮
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if(cancelButton != nil)
    {
        [cancelButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
        [cancelButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
        [cancelButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        cancelButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
        cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cancelButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)
              forControlEvents :UIControlEventTouchUpInside];
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
        if(leftItem != nil)
        {
            self.navigationItem.leftBarButtonItem = leftItem;
        }
    }
    
    //3.导航面板右边的发布按钮
    UIButton* postButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if (postButton != nil)
    {
        [postButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
        [postButton setTitle:@"发布" forState:UIControlStateNormal];
        [postButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        postButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
        postButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [postButton addTarget:self action:@selector(postButtonEventTouchUpInside)
            forControlEvents :UIControlEventTouchUpInside];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:postButton];
        if(rightItem != nil)
        {
            self.navigationItem.rightBarButtonItem = rightItem;
        }
    }
    
    //4.导航面板中部文字
    UILabel* navigationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    if (navigationLabel != nil)
    {
        [navigationLabel setTextColor:[UIColor blackColor]];
        navigationLabel.text = POST_NAVIGATION_TITLE;
        [navigationLabel setTextAlignment:NSTextAlignmentCenter];
        navigationLabel.font = [UIFont systemFontOfSize:18.0];
        self.navigationItem.titleView = navigationLabel;
    }
    
    //5.导航下面的一条分割线
    UIView* line = [[UIView alloc]initWithFrame:CGRectMake(0, 20 + 44,WIDTH_SCREEN, 1)];
    if (line != nil)
    {
        line.backgroundColor = [[UIColor alloc] initWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
        [self.view addSubview:line];
    }

自定义:

    //1.创建导航栏视图
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 65)];
    if (navView != nil)//当导航视图没有加载成功的时候推出该方法
    {
        //1.为导航视图设置背景
        navView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1];
        [[self navigationController] setNavigationBarHidden:YES animated:YES];
        
        //2.导航面板左边的取消按钮
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (leftButton != nil)
        {
            leftButton.frame = CGRectMake(15, 20, 65, 44);
            [leftButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
            [leftButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
            leftButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
            leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)
                forControlEvents :UIControlEventTouchUpInside];
            [navView addSubview:leftButton];
        }
        //3.导航面板右边的发布按钮
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (rightButton != nil)
        {
            [rightButton setFrame:CGRectMake(WIDTH_SCREEN - 80, 20, 65, 44)];
            [rightButton setTitle:@"发布" forState:UIControlStateNormal];
            [rightButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
            rightButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
            rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [rightButton addTarget:self action:@selector(postButtonEventTouchUpInside)
                 forControlEvents :UIControlEventTouchUpInside];
            [navView addSubview:rightButton];
        }
        
        //4.导航面板中部文字
        UILabel* navTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, WIDTH_SCREEN - 80 - 80, 44)];
        if (navTitle != nil)
        {
            [navTitle setTextColor:[UIColor blackColor]];
            navTitle.text = POST_NAVIGATION_TITLE;
            [navTitle setTextAlignment:NSTextAlignmentCenter];
            navTitle.font = [UIFont systemFontOfSize:18.0];
            [navView addSubview:navTitle];
        }
        
        //5.在导航视图底添加分割线
        UIView *navDividingLine = [[UIView alloc] init];
        if (navDividingLine != nil)
        {
            navDividingLine.frame = CGRectMake(0, 20 + 44, WIDTH_SCREEN, 1);
            navDividingLine.backgroundColor = [UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1];
            [navView addSubview:navDividingLine];
        }
        
        //6.往view增加导航栏
        [self.view addSubview:navView];
    }


ios 导航栏(自定义和使用系统方式)

标签:blog   io   color   ar   os   使用   for   div   on   

原文地址:http://blog.csdn.net/coslay/article/details/41211657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!