How do I remove UITabBarItem selectionImage padding? - ios

I am setting the SelectionIndicatorImage as a stretchable image (to accommodate for various device widths) via UITabBar appearance:
UIImage *selectedImage = [[UIImage imageNamed:#"SelectedTab"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[[UITabBar appearance] setSelectionIndicatorImage:selectedImage];
As a result I get a 2pt padding on the screen edges. I am basically trying to use selection indicator as a background for currently selected UITabBarItem.
http://i.stack.imgur.com/qFHEk.png
What would be an easy way to solve this?

If you want to use UITabBarController in your app with selection and unselection feature then you should this code
[[self.tabBarController tabBar] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bg_tabBar" ofType:#"png"]]];
[[self.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bg_tabItem_selected" ofType:#"png"]]];
NSArray *arrTabItems = [[self.tabBarController tabBar] items];
UITabBarItem *tabBarItem = [arrTabItems objectAtIndex:0];
[tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"img_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"img_unselected.png"]];
[tabBarItem setImageInsets:UIEdgeInsetsMake(5, 5, 0, 5)];

I've tried something similar. Ended up this way:
Subclass UITabBarController and add property:
#property(nonatomic, readonly) UIImageView *imageView;
Setup your image as you want:
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabbaritem"]];
[self.tabBar addSubview:self.imageView];
}
Place your subview in correct position:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// get all UITabBarButton views
NSMutableArray *tabViews = [NSMutableArray new];
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:[UIControl class]]) {
[tabViews addObject:[NSValue valueWithCGRect:view.frame]];
}
}
// sort them from left to right
NSArray *sortedArray = [tabViews sortedArrayUsingComparator:^NSComparisonResult(NSValue *firstValue, NSValue *secondValue) {
CGRect firstRect = [firstValue CGRectValue];
CGRect secondRect = [secondValue CGRectValue];
return CGRectGetMinX(firstRect) > CGRectGetMinX(secondRect);
}];
// place your imageView on selected index button frame
CGRect frame = self.tabBar.bounds;
CGSize imageSize = CGSizeMake(CGRectGetWidth(frame) / self.tabBar.items.count, self.imageView.image.size.height);
CGRect selectedRect = sortedArray.count > self.selectedIndex ? [sortedArray[self.selectedIndex] CGRectValue] : CGRectZero;
[self.imageView setFrame:CGRectIntegral(CGRectMake(CGRectGetMinX(selectedRect), CGRectGetMaxY(frame) - imageSize.height,
CGRectGetWidth(selectedRect), imageSize.height))];
}
Then you only need to refresh layout on each tab change:
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
[super setSelectedIndex:selectedIndex];
[self.view setNeedsLayout];
}
- (void)setSelectedViewController:(UIViewController *)selectedViewController {
[super setSelectedViewController:selectedViewController];
[self.view setNeedsLayout];
}
Old answer how to remove padding (this is what I firstly understood as your question, I'm leaving it as a reference for other people).
In iOS 7+ UITabBar has following property:
/*
Set the itemSpacing to a positive value to be used between tab bar items
when they are positioned as a centered group.
Default of 0 or values less than 0 will be interpreted as a system-defined spacing.
*/
#property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
This should enable you to get rid of padding between items.

Related

iOS8 UISearchBar in UIToolbar has no left padding

I have a UIViewController class that contains a UITableView. In the table view header, I have a UIToolbar containing, among other things, a UISearchBar. In iOS8, when I tap on the search bar to search, the search display controller animates the bar to the top of the screen as expected, but the search bar has no margin on the left hand side.
The most stripped down of the code that reproduces is as follows:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, [self tableView].frame.size.width, 44.0)];
if ([toolbar respondsToSelector:#selector(setBarTintColor:)]) {
[toolbar setBarTintColor:[UIColor lightGrayColor]];
}
[[self tableView] setTableHeaderView:toolbar];
UIView *searchBarView = [[UIView alloc] initWithFrame:[[[self searchDisplayController] searchBar] frame]];
[[[self searchDisplayController] searchBar] setBackgroundImage:[[UIImage alloc] init]];
[searchBarView addSubview:[[self searchDisplayController] searchBar]];
[[[self searchDisplayController] searchBar] setText:#""];
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarView];
[toolbar setItems:#[searchBarItem]];
}
Any help / suggestions is greatly appreciated.
Edit: This works correctly on iOS 6.1 and 7.0/1
It seem to be a iOS8 bug.
You can use a temporary solution from this
Basically, you can create a subclass of UIToolbar.
Then in subclass you just created, add this code to append missing space:
#define DEFAULT_APPLE_PADDING 20.0f
-(void)layoutSubviews{
[super layoutSubviews];
[self.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
if ([NSStringFromClass(view.class) hasPrefix:#"UIToolbar"] &&
[NSStringFromClass(view.class) hasSuffix:#"Button"]) {
CGRect buttonFrame = view.frame;
if (buttonFrame.origin.x == 0) {
buttonFrame.origin.x = DEFAULT_APPLE_PADDING;
} else if (buttonFrame.origin.x + buttonFrame.size.width == self.bounds.size.width) {
buttonFrame.origin.x -= DEFAULT_APPLE_PADDING;
}
view.frame = buttonFrame;
}
}];
}

Problems showing badge on UITabBarItem with UIImageRenderingModeAlwaysOriginal image

I'm trying to show a badge on some UITabBarItems that are configured with an image with UIImageRenderingModeAlwaysOriginal. I'm doing this because the desired effect is something like a raised UITabBarItem a-la Instagram. This is the code I'm using to test this (it's in a UITabBarController category):
- (void) replaceImageForTab:(int)itemIndex
defaultImage:(UIImage *)defaultImage
selectedImage:(UIImage *)selectedImage
title:(NSString *)title
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:itemIndex];
[vc.tabBarItem setImage:[defaultImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setSelectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setTitle:title];
[vc.tabBarItem setBadgeValue:#"1"];
}
However, the badges images are shown clipped, like shown in here:
I think is because the badge is being added within the UITabBar's UIView, and it is clipping its contents. But I can't modify that value since that's a private property. What other options do I have here?
I haven't found a way to change the offset in the stock badges, so I had to roll out my own solution. It's not pretty, but it works (this is, again, on a UITabBarController category):
- (void) v_setBadgeValue:(NSString *)badgeValue inTab:(NSInteger)tabIndex
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:tabIndex];
UITabBarItem *tabBarItem = vc.tabBarItem;
UIImage *normalImage = tabBarItem.image;
UIImage *selectedImage = tabBarItem.selectedImage;
UIImage *badgedNormalImage = [[self class] addBadgeWithValue:badgeValue toImage:normalImage];
UIImage *badgedSelectedImage = [[self class] addBadgeWithValue:badgeValue toImage:selectedImage];
[tabBarItem setImage:[badgedNormalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage:[badgedSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
+ (UIImage *) addBadgeWithValue:(NSString *)badgeValue toImage:(UIImage *)image
{
//Here you have to create your badge view. Let's just use a red square for now
UIView *badgeView = [[UIView alloc] initWithFrame:CGRectMake(16, 3, 14, 14)];
[badgeView setBackgroundColor:[UIColor redColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView addSubview:badgeView];
UIGraphicsBeginImageContextWithOptions((imageView.bounds.size), NO, [[UIScreen mainScreen] scale]);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
Now you're in complete control of the badges offset and position, that's important if you, like me, have a very strange UITabBar to work with. This is the result:

Animating UIImageView frame by frame

I have an idea about how to add animated UIImageView using frame by frame method BUT my question is about How to animate UIImageView ALREADY added on view controller storyboard as IBOutlet UIImageView .
what will be changed at this peace of code ?!
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"bookmark1.png"],
[UIImage imageNamed:#"bookmark2.png"],
[UIImage imageNamed:#"bookmark3.png"],
[UIImage imageNamed:#"bookmark4.png"],
[UIImage imageNamed:#"bookmark5.png"],nil];
myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25;
myAnimatedView.animationRepeatCount = 1;
[myAnimatedView startAnimating];
[self.navigationController.view addSubview:myAnimatedView];
I want to animate this image like that over my viewController
http://imageshack.us/a/img717/3051/bookmarkl.gif
It's even easier. First, add in .h file:
#property (nonatomic, retain) IBOutlet UIImageView *iV;
After, connect this outlet to the actual UIImageView on your storyboard. Change your code:
iV.animationImages = myImages;
iV.animationDuration = 0.25;
iV.animationRepeatCount = 1;
[iV startAnimating];
And that's all. Hope this helped
p.s. And yes, don't forget iV = nil; in - (void)viewDidUnload method
UPD: added
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.25];
After startAnimating call, and obviously added animationDone method:
- (void)animationDone {
[iV stopAnimating];
[iV setImage:[UIImage imageNamed:#"bookmark5.png"]];
}
Well it will be pretty much the same. You dont need to allocate your image view [[UIImageView alloc] init] nor do you need to add it to the viewController. The rest is the same.

UISearchBar: clear background color or set background image [duplicate]

This question already has answers here:
How to change inside background color of UISearchBar component on iOS
(26 answers)
Closed 7 years ago.
How can set the background image, or clear the background, of a search bar, like the note application?
A future-proof way:
[searchBar setBackgroundImage:[UIImage new]];
[searchBar setTranslucent:YES];
mj_ has the answer that i used. i was just going to comment as such but i can't yet. So i'll just post my own answer with my implementation where I add a search bar to the top of a table view with a semi-transparent BG.
DLog(#" Add search bar to table view");
//could be a UIImageView to display an image..?
bg = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease];
bg.backgroundColor = [UIColor blackColor];
UISearchBar *sb = [[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 290, 45)] autorelease];
sb.barStyle = UIBarStyleBlackTranslucent;
sb.showsCancelButton = NO;
sb.autocorrectionType = UITextAutocorrectionTypeNo;
sb.autocapitalizationType = UITextAutocapitalizationTypeNone;
sb.delegate = self;
[bg addSubview:sb];
table.tableHeaderView = bg;
for (UIView *subview in sb.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
I had problems w/ the answer above. I used the following.
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
This worked for me (ios4.2+)
// Change the search bar background
-(void)viewWillAppear:(BOOL)animated {
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"search_bar_bg"]];
[self.searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
}
I just came up with a solution that works really well. You have to override the UISearchBar and then hide both the Background and Segment Control layers. Then Draw the background.
# .m
#import "UISearchBar.h"
#import <QuartzCore/QuartzCore.h>
#implementation UISearchBar(CustomBackground)
- (id)init
{
for ( UIView * subview in self.subviews )
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground") ] )
subview.alpha = 0.0;
if ([subview isKindOfClass:NSClassFromString(#"UISegmentedControl") ] )
subview.alpha = 0.0;
}
return self;
}
+ (UIImage *) bgImagePortrait
{
static UIImage *image = nil;
if (image == nil)
image = [[UIImage imageNamed:#"UISearchBarBack.png"] retain ];
return image;
}
+ (UIImage *) bgImageLandscape
{
static UIImage *image = nil;
if (image == nil)
image = [[UIImage imageNamed:#"UISearchBarBack.png"] retain];
return image;
}
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)contenxt
{
if ([self isMemberOfClass:[UISearchBar class]] == NO)
return;
UIImage * image = ( self.frame.size.width > 320 ) ? [UISearchBar bgImageLandscape ] : [UISearchBar bgImagePortrait ];
for ( UIView * subview in self.subviews ) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground") ] )
subview.alpha = 0.0;
if ([subview isKindOfClass:NSClassFromString(#"UISegmentedControl") ] )
subview.alpha = 0.0;
}
CGContextTranslateCTM( contenxt , 0 , image.size.height );
CGContextScaleCTM( contenxt, 1.0, -1.0 );
CGContextDrawImage( contenxt , CGRectMake( 0 , 0 , image.size.width , image.size.height ), image.CGImage );
}
#end
# .h
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface UISearchBar(CustomBackground)
#end
Hope this helps!
You have two questions here:
1.UISearchBar clear background color:
See my answer here
2.Set background image
Solution:(If you are in iOS 5.0 +)
[[UISearchBar appearance]setSearchFieldBackgroundImage:[navBarGradImage resizableImageWithCapInsets:inset2] forState:UIControlStateNormal];
NOTE: You can also try using a transparent image as a background.
Hope this helps.
I prefer to just set the alpha to 0 so you can hide/show on demand.
// Hide
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:0.0];
// Show
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:1.0];
How to set background color in UISearchBar:
#import <QuartzCore/QuartzCore.h>
Then make an outlet connection to search bar (say, objSearchbar), and use these lines :
for (UIView *subview in self.objSearchbar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
self.tweetSearchbar.layer.backgroundColor = [UIColor blueColor].CGColor;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
Look here: UISearchbar background image change
with iOS8 sdks apple moved #"UISearchBarBackground" view one level deeper, so have will need to look at subviews of the child-views as bellow,
for (UIView *subview in searchBar.subviews) {
for(UIView* grandSonView in subview.subviews){
if ([grandSonView isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
grandSonView.alpha = 0.0f;
}else if([grandSonView isKindOfClass:NSClassFromString(#"UISearchBarTextField")] ){
NSLog(#"Keep textfiedld bkg color");
}else{
grandSonView.alpha = 0.0f;
}
}//for cacheViews
}//subviews
Set background image
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:searchBar.bounds];
backgroundView.image = [UIImage imageNamed:#"xxxx.png"];
[searchBar insertSubview:backgroundView atIndex:1]; // at index 1 but not 0
[backgroundView release];
One liner:
[[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview];

Can I set image as a title to UINavigationBar?

Is it possible to set some image as title of Navigation bar?
I think NYTimes application used a Navigation bar and title is look like image file (the reason why it's seems UINavigationBar is because they use right button to search).
You can use an UIImageView for the UINavigationItem.titleView property, something like:
self.navigationItem.titleView = myImageView;
I find that a transparent .png at about 35px in height has worked well.
- (void)awakeFromNib {
//put logo image in the navigationBar
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = img;
[img release];
}
You can do it right from storyboard (as of Xcode 7):
Create a view outside main view of view controller. It can be a nested view or just an image
Add navigation item to your view controller
Ctrl+ drag from navigation item and drop on outside view
4.select title view
I have created a custom category for UINavigationBar as follows
UINavigationBar+CustomImage.h
#import <UIKit/UIKit.h>
#interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
- (void) removeIfImage:(id)sender;
#end
UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
#implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
if (image == NULL) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(110,5,100,30);
[self addSubview:imageView];
[imageView release];
}
- (void) clearBackgroundImage {
NSArray *subviews = [self subviews];
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
#end
I invoke it from my UINavigationController
[[navController navigationBar] performSelectorInBackground:#selector(setBackgroundImage:) withObject:image];
This line will work for you, I always use this
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"imageNavBar.png"] forBarMetrics:UIBarMetricsDefault];
I modified the UINavigationBar+CustomImage.m to have the title still visible to the user. Just use insertSubview: atIndex: instead of addSubview:
UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
#implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
if (image == NULL) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self insertSubview:imageView atIndex:0];
[imageView release];
}
- (void) clearBackgroundImage {
NSArray *subviews = [self subviews];
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
#end
This also works well too
[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"YourLogo"]]];
Do it quickly using storyboard and #IBDesignable:
#IBDesignable class AttributedNavigationBar: UINavigationBar {
#IBInspectable var imageTitle: UIImage? = nil {
didSet {
guard let imageTitle = imageTitle else {
topItem?.titleView = nil
return
}
let imageView = UIImageView(image: imageTitle)
imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
imageView.contentMode = .scaleAspectFit
topItem?.titleView = imageView
}
}
}
Then in attributes inspector just select an image:
and wait a second for result:
So setting view is there where it should be... in storyboard.
For those who have the same error but in Xamarin Forms, the solution is to create a Renderer in iOS app and set the image like so :
[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyApp.Renderers.NavigationPageRenderer))]
namespace MyApp.Renderers
{
#region using
using UIKit;
using Xamarin.Forms.Platform.iOS;
#endregion
public class NavigationPageRenderer : PageRenderer
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetTitleImage();
}
private void SetTitleImage()
{
UIImage logoImage = UIImage.FromFile(ResourceFiles.ImageResources.LogoImageName);
UIImageView logoImageView = new UIImageView(logoImage);
if (this.NavigationController != null)
{
this.NavigationController.NavigationBar.TopItem.TitleView = logoImageView;
}
}
}
}
Hope it helps someone!
I modified the UINavigationBar+CustomImage code to properly work without leaking memory.
- (void)setBackgroundImage:(UIImage *)image
{
if (! image) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:imageView];
[imageView release];
}
- (void) clearBackgroundImage
{
// This runs on a separate thread, so give it it's own pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *mySubviews = self.subviews;
// Move in reverse direction as not to upset the order of elements in the array
for (int i = [mySubviews count] - 1; i >= 0; i--)
{
if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
{
[[mySubviews objectAtIndex:i] removeFromSuperview];
}
}
[pool release];
}
The following is how you would do this in (Xamarin's) MonoTouch with C#.NET
Create a UIViewConvrtoller that is in a NavigationController then call this at any time:
someNiceViewControllerYouMade.NavigationController.NavigationBar
.InsertSubview(new UIImageView
(MediaProvider.GetImage(ImageGeneral.navBar_667x44)),0);
Note: MediaProvider is just a class that fetches images.
This example allows for the view to fill the full Navigation Bar , and lets the text for the items caption appear too.
If your buttons disappear when you navigate back and forth the navigation, this fixed it for me:
NSArray *mySubviews = navigationBar.subviews;
UIImageView *iv = nil;
// Move in reverse direction as not to upset the order of elements in the array
for (int i = [mySubviews count] - 1; i >= 0; i--)
{
if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
{
NSLog(#"found background at index %d",i);
iv = [mySubviews objectAtIndex:i];
[[mySubviews objectAtIndex:i] removeFromSuperview];
[navigationBar insertSubview:iv atIndex:0];
}
}
Just use
[navController.navigationBar insertSubview:myImage atIndex:0] ;
where myImage is of type UIImageView
and navController is of type UINavigationController
ios5.0 introduced a heap of features to customise the appearance of standard elements. If you didn't want to use an ImageView for the title, an alternative would be to customise the appearance of all UINavbars using a background image and a custom font/colour.
- (void) customiseMyNav
{
// Create resizable images
UIImage *portraitImage = [[UIImage imageNamed:#"nav_bar_bg_portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *landscapeImage = [[UIImage imageNamed:#"nav_bar_bg_landscape"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];
// set the title appearance
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:50.0/255.0 green:150.0/255.0 blue:100/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
}
In MonoTouch you can use this:
this.NavigationItem.TitleView = myImageView;
Add image to naviagtionBar with SWIFT that scales to fit and clips to bounds. You can call this function inside the view controllers viewDidLoad() function.
func setupNavigationBarWithTitleImage(titleImage: UIImage) {
let imageView = UIImageView(image: titleImage)
imageView.contentMode = .ScaleAspectFit
imageView.clipsToBounds = true
navigationItem.titleView = imageView
}

Resources