iOS项目开发经验:【常用代码5】

<5-1> 自定义Navigation导航的titleView

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor];   
titleLabel.font = [UIFont boldSystemFontOfSize:20];  
titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1]; 
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = @"Custom Title";  
self.navigationItem.titleView = titleLabel;


 

<5-2>Navigation Bar设置背景颜色

self.navigationController.navigationBar.tintColor = 
        [UIColor colorWithRed:(200.0/255.0) green:(200.0 / 255.0) blue:(250.0 / 255.0) alpha:1];


<5-3>Navigation Bar设置背景图片

(1)iOS 5.0

在viewdidLoad中增加:
if ([self.navgationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        UIImage *image= [UIImage imageNamed:@"navBar_bg.png"]; 
        CGSize navBarSize = self.navigationController.navigationBar.bounds.size;  
        image = [self scaleImage:image ToSize:navBarSize]; ///<5-4>方法
         [self.navigationController.navigationBar setBackgroundImage:image  forBarMetrics:UIBarMetricsDefault]; 
   }

(2)低于iOS5.0版本

 

自定义navigationBar,设置背景方法:
@implementation UINavigationBar (CustomImage)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"navBar_bg.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
}
@end

<5-4>缩放图片到制定的大小

+(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


<5-5>使用Push和Pop打开和关闭ViewController

[self.navigationController pushViewController:viewController animated:YES];
[self.navigationController popViewControllerAnimated:YES];




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值