UINavigation category 定制

旧有模块清理,把自定义的navigationbar 改成 category。

具体的还需要自己填充。

以下方法尚有问题,请不要使用,空闲下来就重新修正


如下


UIViewController+XM_Nav.h

#import <UIKit/UIKit.h>

@interface UIViewController (XM_Nav)

-(void)XM_SetTitleText:(NSString *)title;

-(void)XM_SetButtonImage:(NSString *)image isRight:(BOOL)isRight Target:(id)target Selector:(SEL)selector;

-(void)XM_SetBgImage:(NSString *)image;

@end

UIViewController+XM_Nav.m

-(void)XM_SetTitleText:(NSString *)title{
    self.navigationController.navigationBar.topItem.title = title;
    
    UIFont* font = [UIFont fontWithName:@"Arial-ItalicMT" size:21.0];
    NSDictionary* textAttributes = @{NSFontAttributeName:font,
                                     NSForegroundColorAttributeName:[UIColor blackColor]};
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:textAttributes];
}

-(void)XM_SetButtonImage:(NSString *)image isRight:(BOOL)isRight Target:(id)target Selector:(SEL)selector
{
    
    UIButton *abutton = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
    [abutton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [abutton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    
    CGSize navSize = self.navigationController.navigationBar.frame.size;
    CGSize butSize = abutton.frame.size;
    if (isRight) {
        abutton.frame = CGRectMake(navSize.width- 20 - butSize.width, 0.0, butSize.width, butSize.height);
    }else{
        abutton.frame = CGRectMake(20.0, 0.0, butSize.width, butSize.height);
    }
    [self.navigationController.navigationBar addSubview:abutton];
    
}

-(void)XM_SetBgImage:(NSString *)image{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
    self.navigationController.navigationBar.topItem.titleView = imageView;
}


你可能感兴趣的:(定制,UINavigationBar,category)