setting image in navigation bar - objective-c

Hi I'm new in objective C and using navigation controller in my app. Where I want to set an icon image and title of screen. like
How can I do this. I've used below code but it does not work as I want. It shows only image in navigation Bar.
self.navigationController.title = NSLocalizedString(#"Concate Your Memories", nil);
UIImageView* tileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TopBar-icon.png"]];
tileImageView.frame = CGRectMake(0, 0, 60, 25);
self.navigationItem.titleView = tileImageView;
Please help me. Thanks in advance

The code you have given and everyone else have suggested is correct. But you're assigning the image and title separately. Therefore,
Create your own view with your image and a label and assign it as the title view OR
Add the title to the image itself and assign it to the titleview like as you are doing now

For Setting Image in Navigation Bar
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TopBar-icon.png"] forBarMetrics:UIBarMetricsDefault];

that code works properly on iOS7:
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]]];
(tested on real device and simulator as well)

You can create your own view and set it on the top of screen.
Hide default navigation bar for the whole app.

Related

Adjust SearchBar size in Navigation Bar when Orientation changes to Landscape iOS7

I'm using a Storyboard where I have a UIViewController embedded in a Navigation Controller. I have placed a SearchBar into the Navigation Controller "replacing" the TextView item with it. It works as expected in portrait.
The problem is when I rotate it to Landscape, where the Searchbar remains in the center and its height seems to be bigger than the Nav Bar.
I have tried configuring the constraints, but it hasn't been successful.
Could you help me please?
Set Constraints to your Search bar.. It will shows the magic.
I fixed it adding the search bar in the title view, this way:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[self.searchBar setPlaceholder:#"Buscar Recetas"];
[self.searchBar sizeToFit];
[self.searchBar setDelegate:self];
[self.navigationItem setTitleView:self.searchBar];
Note that I'm not using a search display controller in this case. Search Display Controller already has a property where you can set the search bar in the navigation bar, like this:
self.searchdisplaycontroller.displaysSearchBarInNavigationBar = YES;

Image as a title in NavigationItem

I have a tab controller with four items in it. Second item is a navigation controller with a table view. I want to place logo of my app instead of title. Finally I can show my logo, but I can't understand how to scale it down to the size of Navigation Bar. Size of my image is 59x68px.
I do the followings thing in MyTableViewController viewDidload
[super viewDidLoad];
self.navigationController.navigationBar.translucent= NO;
UIImage *image = [UIImage imageNamed:#"assorti_logo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView = myImageView;
I tried a lot of solutions from here, and non works. Is there any way to scale this image to match navigation bar height. Or what size of image should I use to see correct image both on iPhone 4s and 5?
I've finally found out the problem. I needed only to rename my image file to "logo#2x.png" The key is "#2x" in the end.

Setting UIButton image results in blue button in iOS 7

On iOS 6 SDK I wrote the following lines of code to display an image inside a button:
NSURL *thumbURL2 = [NSURL URLWithString:#"http://example.com/thumbs/2.jpg"];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];
But now with Xcode 5 and iOS 7 this doesn't work. The button doesn't contain the image. The button is filled with blue color.
In iOS7 there is new button type called UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
Check your .xib file and change button type to Custom
To do this programmatically, add this line to the viewDidLoad:
[UIButton buttonWithType:UIButtonTypeSystem];
It seems iOS 7 is using the image provided just as an Alpha mask for displaying the button's tint color.
Changing the button type to UIButtonTypeCustom did the trick for me (thanks user716216!).
Setting the image as background doesn't always work if you already have a background image, as was my case.
Swift 3, 4, 5 :
let image = UIImage(named: "my-image")
myButton.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
There's a good chance that the image is there and you just can't see it. Try changing the button's type to UIButtonTypeCustom. If that doesn't work, set the button's background color to [UIColor clearColor];
For swift:
let aButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
The issue is the TintColor. By default, iOS throws a blue tint color over every button. You can get around it through 3 ways.
Change the tint color. [button setTintColor:[UIColor blackColor]];
This may color your image in ways you don't want it to.
As most other suggested, set the background image. [button setBackgroundImage:[UIImage...]];
Add an UIImageView to your button.
UIImageView * img = [[UIImageView alloc] initWithImage:[UIImage...]];
[button addSubView:img];
I had the same issue.
On my storyboard I had a button without any image.
I would then assign the image in the code.
IOS 7 came and I got a lot of blue images.
The resolution was simple yet confusing. If I assign any image on the storyboard and then change the image at run time it works fine.
You always must specify a starting image on the storyboard even if you are not going to use it.
This worked for me
[myButton1 setBackgroundImage:[UIImage imageNamed:#"phones.png"] forState:UIControlStateNormal];
Note:Remove front image before doing this.
Old thread, but I wanted to chime in because I just had the same problem. The issue was just that you are calling setImage when you should call setBackgroundImage.
In iOS 13 -- just set the Tint property to White, while keeping the type of the UIButton as Custom
None of the given solutions were working for me. If you do not set an initial image in Storyboard, you can still change the image of the button by using setBackgroundImage.
For your example, only a minor change is needed.
NSURL *thumbURL2 = [NSURL URLWithString:#"http://example.com/thumbs/2.jpg"];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setBackgroundImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];
This Problem is called blue color problem of the button in xcode.
When we make button by code the button shows the blue tint color by default.This can be solved byt assigning tint color to black or white accordingly to your cell's color.
The code is :
UIImage *closebtnimg = [UIImage imageNamed:#"icon_uncheck.png"];
UIImage *closebtnimg1 = [UIImage imageNamed:#"icon_checked.png"];
Custombutton *button = [Custombutton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(52, 66, 25, 24)];
[button setBackgroundImage:closebtnimg forState:UIControlStateNormal];
[button setBackgroundImage:closebtnimg1 forState:UIControlStateSelected];
[button setTintColor:[UIColor whiteColor]];
[cell.contentView addSubview:button];
[button addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
Using Xcode 9.2 none of the above solutions worked for what I was looking for.
I was looking for a solution that will let me set .normal and .selected UIControlState images inside the storyboard for their original rendering mode, but, inside the Swift file, no string literals should exist regarding the image names.
Basically, inside your code you will get the image you set inside your storyboard for .normal state and re-render it as .alwaysOriginal (Same for .selected state), then, you will set that image (which is now rendered as original and won't be affected by the tint) for the relevant state (.normal and .selected) of your UIButton.
Here it is:
// Get your .normal image (you set via your storyboard) and render it as original
let unselectedImage = yourButton.image(for: .normal)?.withRenderingMode(.alwaysOriginal)
// Set your normal image but this time rendered as original
yourButton.setImage(unselectedImage, for: .normal)
// Same for selected state
let selectedImage = yourButton.image(for: .selected)?.withRenderingMode(.alwaysOriginal)
yourButton.setImage(selectedImage, for: .selected)
This way you can set your button image states and if the image name will change, it won't affect your code.
making the tint color as clearcolor for all the four states(Default,Highlighted,selected,disabled) worked for me.
In Swift 4, initialize your UIButton and assign uyour image Data as follows:
let myButton = UIButton(type: .cutsom)
myButton.setImage(UIImage(data:myImageData), for: .normal)

UINavigationBar with customized background - how to make rightBarButton visible

I've customized background of navigation bar in my RootViewController (just part of code)
[navBar insertSubview:customBack atIndex:0];
I push detailViewController and add activity indicator as a rightBarButtonItem
UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.activityIndicator = actInd;
[actInd release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];
[self.navigationItem setRightBarButtonItem:barButton];
[barButton release];
The problem is that the indicator is not visible but without customized background it works OK.
You can better change your navigationBar Background I think. Create a subclass of UINavigationBar and add:
- (void)drawRect:(CGRect)rect {
UIImage * image = [UIImage imageNamed:#"MyNavigationBarBackground.png"];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0, -1.0); // Otherwise the image is drawn upside-down
CGContextDrawTiledImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
}
I do that and I never add a problem with any button :)
Zoleas has the right idea. If you don't or can't use a subclass, I think your problem is that you are adding your subview at index 0 so it is on top of the other views in the navigation bar, it is probably hiding your buttons.
set rightBarButtonItem with following way
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
Try this and also check the position of background
The activity indicator is there its just not being activated. Try animating it like this:
[actInd startAnimating];
Or if you want to keep it visible set the hideWhenStopped property like this:
actInd.hidesWhenStopped = NO;
Did you try to change the origin from the UIActivityIndicatorView?
Or creating it inside an UIView and add this UIView to the UIBarButtonItem.
I think the problem can be the frame from the element.
By this way you can resolve the problem:
Create activity indicator and add in a view1
Now this view1 add or create navigation bar with the help of this view1. Now when view1 is visible automatically you will be able to see the loading indicator.
Just wild try on this.

Objective C: How to change text color in navigation bar

I have changed my navigation bar color via the following code
navconFvc.navigationBar.tintColor = [UIColor colorWithHexString:#"faf6f5"];
The code worked but the text color also needs to be changed (see screenshot below). Also the refresh button logo on the right is affected as well
The same issue occurs if I navigate to another page in the stack
Question: How can I change the color of the
title text
Back button text and
right bar button icon color?
After I changed the background color of the navbar?
In iOS 7, just use:
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
Change [UIColor whiteColor] with whatever text color you want
For the title here's the way:
iPhone Navigation Bar Title text color
And for the custom buttons here's the way:
adding buttons to ui navigation controller bottom bar
To change text color:
_navController.navigationBar.titleTextAttributes
= #{UITextAttributeTextColor : [UIColor blackColor]};
Adding refresh button and color it:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(reload)];
[button setTintColor:[UIColor blackColor]];
self.navigationItem.rightBarButtonItem = button;
Variables that effect navigation bar background:
_navController.navigationBar.backgroundColor = [UIColor whiteColor];
_navController.navigationBar.tintColor = [UIColor whiteColor];
_navController.navigationBar.translucent = NO;
I just put together a simple UIViewController subclass that adds a customizable back button that allows you to change text colors. It basically adds some willAppear/willDisappear logic to animate the back button the way the UINavigationController does while using the leftBarButtonItem property. You might extend this to also do the rightBarButtomItem as well.
https://github.com/typeoneerror/BBCustomBackButtonViewController