Image as a title in NavigationItem - objective-c

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.

Related

trying to set an image as button image after picking the image from uiimagepicker

i have tried to get an answer to this question for a while. what i am trying to do is fire the UIImagePickerController and either choose from camera or select from goto library. this part works just fine. then i want to set that image as the button image on top of the button i am using to fire the image picker. in iOS 6 that was working just fine. since iOS 7 the image does not get set as the button image. i have tried many links and read many posts and went through even dev forum with no success in finding an answer. this is the last part of my desperate attempt to find an answer before going to apple technical support. i am hoping someone would be able to help me out. below is the code i am using to set the image to the button after selecting it from UIImagePickerController.
- (IBAction)choosePhoto
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (IBAction)takePhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (imagePicker) {
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.choosePhotoButton setImage:self.image forState:UIControlStateNormal];
} else {
if (picker) {
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.takePhotoButton setImage:self.image forState:UIControlStateNormal];
}
}
[self dismissViewControllerAnimated:YES completion:nil];
imagePicker = nil;
}
currently after selecting either button the image set on it is the tint color of the button as it shows in the screen shot below. the two white squares are the buttons where the picked image should be.
can someone take a look at my code and help me solve this issue?
You should look at the document called "Buttons" that's referenced in the UIButton Class Reference. When you add an image to a button (System type), it's added to the left of the title and as a template image, unless you set the rendering mode of the image to UIImageRenderingModeAlwaysOriginal. So, if you want the image to the left, you can do this:
- (IBAction)setImageForSender:(id)sender {
UIImage *buttonImage = [UIImage imageNamed:#"pic2.jpg"];
buttonImage = [buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[sender setImage:buttonImage forState:UIControlStateNormal];
}
If you want the image under the title (and centered in the button) set the background image instead. This shows up normally, you don't have to deal with the rendering mode.
- (IBAction)setImageForSender:(id)sender {
UIImage *buttonImage = [UIImage imageNamed:#"pic2.jpg"];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
}
Here is the relevant portion of that "Buttons" document:
Images
Using the Image (currentImage) field, you can specify an image to
appear within the content of your button. If the button has a title,
this image appears to the left of it, and centered otherwise. The
image does not stretch or condense, so make sure to select an image
that is the proper size to appear in your button. Note that this image
will be automatically rendered as a template image within the button,
unless you explicitly set its rendering mode to
UIImageRenderingModeAlwaysOriginal. For more information, see Template
Images.
The Background (currentBackgroundImage) field allows you to specify an
image to appear behind button content and fill the entire frame of the
button. The image you specify will stretch to fill the button if it is
too small. It will be cropped if it is too large.
That the last piece of information in the text above about the image being cropped if it's too large, only happens if you set a specific size for your button. If its size is determined by its intrinsic content size (that's the default if you just drag out a button in IB and don't give it size constraints), it will expand to the size of the image.

UITabBar unselected icon tint

I am trying to change the color of my tab bar icons when the tabs are UNselected. Right now the color is default grey and I can change the color to whatever color I want for when it IS selected.
Apple's dev library said to change the image rendering to "original" instead of its default mode "template." I did that. then it says to use initWithTitle:image:selectedImage: I tried to do that as well but I think that's where I messed up. I wrote this in my viewcontroller.m file. What's wrong here?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
stat = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
(instancetype)initWithTitle:(NSString *)nil image:(UIImage *)stat selectedImage:(UIImage *)stat;
}
The problem is that you are using the same UIImage with UIImageRenderingModeAlwaysOriginal in both places.
Your code should look something like
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
UIImage *statAlwaysOriginal = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:statAlwaysOriginal selectedImage:stat];
The other thing is that there are some actual syntax errors in your post (in the UITabBarItem initialization, but I suspect you just pasted it incorrectly.

Using UIScrollView, UIImageView and Zooming with a single code for different views

I am very new to programming. I am currently developing an iOS app that uses UIButtons to access different UIViews. Each one contains a UIScrollView containing a UIImageView.
The purpose is to have a zoomable, scrollable and panable picture.
I found a very good example of how to do all of these, but the code declares an image with #
so I would have to create several UIViews and generate code for each to manage the specific UIImage (because all have different names) and UIScroll. It is not practical. What I need is to have a single UIViewContoller for different views and a single code that handles the views with different pictures.
This is the code that sets up the image to scroll & zoom and adds it to the scroll view (although I want to include the images visually through the storyboard and not by code).
- (void)viewDidLoad {
[super viewDidLoad];
// Title
self.title = #"Image";
// Set up the image to scroll & zoom and add it to the scroll view
UIImage *image = [UIImage imageNamed:#"photo1.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.scrollView addSubview:self.imageView];
Please help!!
Thank you.

UILabel scaling with UIScrollView

I have a UIScrollView that displays and Image and it scrolls fine and everything. What I want to do is add a UILabel to the UIScrollView to display the title of the image. I managed to do that, but when I zoom out the UILabel does not zoom with the scroll View and stays in the same place on the screen. How would I make it so the label scales with the scrollView Image? Here is the code I have:
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView.delegate = self;
//This just creates a image from a URL
NSURL * photoURL = [FlickrFetcher urlForPhoto:self.photoCellName format:2];
NSData * photoData = [NSData dataWithContentsOfURL:photoURL];
self.imageView.image = [UIImage imageWithData:photoData];
//Setting up scroll View
self.scrollView.contentSize= self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
NSLog(#"Name = %#", [self.photoCellName valueForKeyPath:#"description._content"]);
//Assigning title to the label
self.textLabel.text = [self.photoCellName objectForKey:#"title"];
Make sure label is the subview of your scrollview.
I'm guessing you are providing the UIImageView as the View that will be zoomed by the UIScrollView by implementing the method in the UIScrollViewDelegate.
If not, I'm not sure how your zooming is working then. If you are providing it, you'll have to return a UIView that contains as subviews your UILabel and your UIImageView and you will have to manually apply transformations to the UIView to resize it.
I guess that a similar question was answered in this SO thread, and Dimme (the one that answered it)provided a complete solution with source code, hope it helps!
You should check if you setup autoresizingMask property of your UILabel
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
You can do it in IB too. Then if you will change the frame of its superview the frame of your label will be updated during - (void)setNeedsLayout handling process
... and DO NOT block the main thread creating images from url in - (void)viewDidLoad!

Set title/icon etc to TabBarItem created in IB, through code?

I have a TabBar that I've created through IB, I chose "create new project" -> "Tab bar application". Is there a way for me to access one of the TabBarItems for customization through the code?
It seems to me that something like:
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#"Button one"];
should set the title of that item to "Button one", but it doesen't. The title itself is not a problem (I can set that through IB aswell), however adding an Icon seems to be.
So to sum up, what I really want to know is: Is there a way to add an Icon to a TabBarItem created through IB?
SOLUTION:
Adding in viewDidLoad in the first view, being loaded automatically upon starting the app:
UITabBarController *tb = [self tabBarController];
[[tb.tabBar.items objectAtIndex:1] setTitle:#"Title"];
Let me set the title of the second button (objectAtIndex: 1). I was also able to set the image the same way, which also worked for buttons one (objectAtIndex: 0) and three (objectAtIndex: 2).
Add this to your viewDidLoad: method of one of the tabBar viewControllers and it should work:
- (void)viewDidLoad
{
[super viewDidLoad];
//Get the tabBarItem
UITabBarItem *tbi = [self tabBarItem];
//Give it a lable
[tbi setTitle:#"Title A"];
//create a image from a file for the tabBar
UIImage *i = [UIImage imageNamed:#"NiceImage.png"];
//and put it on the tabBar
[tbi setImage:i];
}
You should be able to set the image and title properties on the TabBarItems:
UITabBarItem *item = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0];
item.image = [UIImage imageNamed:#"home.png"];
Don't forget that the UITabBar only uses the alpha values out of the image you set, so if you don't have an alpha channel in the image you may not see anything when you set an image on the tab bar item.
I've never created a tab bar through IB (always through code), however to set title and icon I use
controller.title = #"Controller";
controller.tabBarItem.image = [UIImage imageNamed:#"image.png"];
where controller is the UIViewController added to the viewControllers' array of the UITabBarController.