UINavigationBar自定义导航栏

#pragma mark --导航栏 或自定义view 

/*

 * 创建导航栏    ! PS::::navbar只能设置背景图片,不能设置背景颜色,不然会造成有一层遮罩层。建议纯色绘制图片代替背景

 */

-(void)createNavBar{

    //创建一个导航栏

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,64)];

//    navBar.backgroundColor = [UIColor clearColor];

    UIImage *image = [UIImage imageWithColor:[UIColor orangeColor] size:CGSizeMake(640, 90)];

    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

    //创建一个导航栏集合

    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];

    

    UIButton* leftBut = [UIButton buttonWithType:UIButtonTypeCustom];

    leftBut.frame = CGRectMake(0, 0, 14, 23);

    [leftBut setBackgroundImage:[UIImage imageNamed:@"NavBackBtn_Nor.png"] forState:UIControlStateNormal];

    [leftBut addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];

    //创建一个左边按钮

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithCustomView:leftBut];


    //设置导航栏的内容

    [navItem setTitle:@"忘记密码"];

    

    //设置 UINavigationBar的标题字体颜色和大小

     [navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont systemFontOfSize:20.0], UITextAttributeFont, [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,nil]];

    

    

    //把导航栏集合添加到导航栏中,设置动画关闭

    [navBar pushNavigationItem:navItem animated:NO];

    

    //把左右两个按钮添加到导航栏集合中去

    [navItem setLeftBarButtonItem:leftButton];

    

    //将标题栏中的内容全部添加到主视图当中

    [self.view addSubview:navBar];

}



你可能感兴趣的:(导航栏,UINavigationBar)