1. 程式人生 > >IOS讓標題欄背景透明、設定左右baritem

IOS讓標題欄背景透明、設定左右baritem

//標題欄背景透明

+(void)setNavigationBarTranslucent:(UIViewController*)vc{

[vc.navigationController.navigationBarsetBackgroundImage:[UIImagenew] forBarMetrics:UIBarMetricsDefault];

vc.navigationController.navigationBar.shadowImage=[UIImagenew];

vc.navigationController.navigationBar.translucent=YES;

}

//自定義image的rightitem

+(UIBarButtonItem*)addRightBarItemWithImage:(UIImage*)imagetarget:(id)targetaction:(SEL)action{

@autoreleasepool{

UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];

button.frame=CGRectMake(0,0, image.size.width,image.size.height);

[buttonsetImageEdgeInsets:UIEdgeInsetsMake(12,5, 12,-6)];

[button

setImage:imageforState:UIControlStateNormal];

button.imageView.contentMode=UIViewContentModeScaleAspectFit;

[buttonaddTarget:targetaction:actionforControlEvents:UIControlEventTouchUpInside];

UIViewController*controller=target;

UIBarButtonItem*item=[[UIBarButtonItemalloc] initWithCustomView:button];

controller.navigationItem.rightBarButtonItem=item;

returnitem;

}

}


//自定義title的rightitem

+(void)addRightBarItemWithText:(NSString*)textcolor:(UIColor*)colortarget:(id)targetaction:(SEL)action{

@autoreleasepool{

UIBarButtonItem*item1=[[UIBarButtonItemalloc] initWithTitle:textstyle:UIBarButtonItemStylePlaintarget:targetaction:action];

UIViewController*controller=target;

[item1setImageInsets:UIEdgeInsetsMake(5,5, 5,5)];

[item1setTintColor:color];

controller.navigationItem.rightBarButtonItem=item1;

}

}


//自定義image的leftitem

+(void)setLeftBarItemWithImage:(UIImage*)imagetarget:(id)targetaction:(SEL)action{

UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];

button.frame=CGRectMake(0,0, image.size.width,image.size.height);

[buttonsetImageEdgeInsets:UIEdgeInsetsMake(10,-18,10, 0)];

[buttonsetImage:imageforState:UIControlStateNormal];

button.imageView.contentMode=UIViewContentModeScaleAspectFit;

[buttonaddTarget:targetaction:actionforControlEvents:UIControlEventTouchUpInside];

UIViewController*controller=target;

UIBarButtonItem*item=[[UIBarButtonItemalloc] initWithCustomView:button];

[controller.navigationItemsetLeftBarButtonItem:item];

}


//自定義title的leftitem

+(UIBarButtonItem*)setLeftBarItemWithTitle:(NSString*)titlecolor:(UIColor*)colortarget:(id)targetaction:(SEL)action{

UIBarButtonItem*item=[[UIBarButtonItemalloc] initWithTitle:titlestyle:UIBarButtonItemStylePlaintarget:targetaction:action];

UIViewController*controller=target;

[itemsetTintColor:color];

[controller.navigationItemsetLeftBarButtonItem:itemanimated:YES];

returnitem;

}