UISearchBar in right of UINavigationBar in objective c - iphone

I created an iPad application in which I would like to have my searchBar to be positioned in the right of a navigation bar.
When I add my search bar in my navigation bar, it is positioned in the middle of the navigationBar. In the middle, instead, I want to show a specific image.
How can I achieve this?
Here is my code snippet,
[self.navigationController.navigationBar setTintColor:[[UIColor alloc]initWithRed:150.0 / 255 green:22.0 / 255 blue:22.0 / 255 alpha:0.0]];
UIImage *image = [UIImage imageNamed:#"logo(1).png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationItem setTitleView:imageView];
self.navigationItem.titleView = sBar;
[titleView release];
With the code above, I am able to see only the searchBar (positioned in the middle) in my simulator.

Try this:
UISearchBar *searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 40)] autorelease];
searchBar.backgroundImage = [[[UIImage alloc] init] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:searchBar] autorelease];
You can always change the frame. Always keep in mind that the backgroundImage property is only available in iOS 5. You should search for alternative options to get rid of the background for iOS 4.x
Let me know if that works for you.

try:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:sBar];

I screwed around with this and I too couldn't get things to align properly (after adding a couple of buttons next to the search bar).
Until anyone suggests a better solution, you are probably better off just hiding the NavigationBar ([self.navigationController setNavigationBarHidden:YES]) in that view and replacing it with a UIView with all the UI elements you need. I think the behavior is more predictable that way.

Related

Moving a Navigation Bar title

I was wondering if it's possible to move my navigationController title closer to the top of the bar? Right now it looks like this.
I was thinking it would be
self.navigationItem.titleView = [[titleView alloc] initWithFrame:CGRectMake(100,2, 2,100)]];
But I guess titleView has to be another view?
Thanks for the help!
Give frame at the time of initialisation!
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;

Use Image for backBarButtonItem with NO TEXT

I'm simply trying to set the backBarButtonItem for my navigation controller to this image
instead of the Apple default arrow button whose title is the same as the previous view controller's title. The closest I've gotten so far is the above image stretched horizontally with the title still appearing overlaid. To get that, I used this code in my AppDelegate.
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
How can I get rid of the title (and prevent the button from being stretched)?
When you use appearance, you are setting the background image of the back bar button items of your app. The fact that they have a background image has nothing to do with whether or not there is a title displayed on them. To use a custom bar button item instead of the default back item, look at this question.
In your case, you may want to not use appearance at all and instead create a bar button item as in the link above but use -initWithImage:style:target:action: instead of -initWithTitle:style:target:action:
you can set custom Image of BarbuttonItem like :-
- (void)viewDidLoad
{
UIImage* imageRight = [UIImage imageNamed:#"Home_btn.png"];
CGRect frameimgRight = CGRectMake(100, 100, 50, 30);
RightBtn = [[UIButton alloc] initWithFrame:frameimgRight];
[RightBtn setBackgroundImage:imageRight forState:UIControlStateNormal];
[RightBtn addTarget:self action:#selector(ActionhomeBtn)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
self.navigationItem.leftBarButtonItem = btnRight;
[super viewDidLoad];
}
Look like:-
Hope its halp's you
I know this method works, and should be called in ViewDidLoad:
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-button.png"] style:UIBarButtonItemStyleBordered target:nil action:#selector(methodName)];
which then has to be assigned to the nav bar:
item.rightBarButtonItem = backButtonItem;
and then pushed:
[self.navBar pushNavigationItem:item animated:NO];

Adding backgroundimage to my app

I am using this code to add a background image to my tableview
myTable.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
on the internet they say this code works, but it's not working for me
can someone help me please?
if its a UITableViewController, as I believe it is, do this:
[myTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or you can do (on a non-table view controller)
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or on viewDidLoad you can do:
UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
[self.view addSubview:backgroundView];
but thats a bit messy - I prefer to use setBackgroundColor:
The problem is that backgroundview for tableview is a UIView, not a UIImage. So you need to create a view, easiest way is to use a UIImageView. For example this would put your splash screen image as the background view for the table in a UITableViewController...
UIImage *backImage = [UIImage imageNamed:#"default.png"];
UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage];
self.tableView.backgroundView = backImageView;
The methods mentioned above are pre the introduction of tableview property backgroundview in 3.2
edit - dear god I was obviously having a coffee deficit today :) The op is indeed using a UIImageView! The only thing I can think is that he is targeting a pre 3.2 platform...

How can I set a background image to navigation controller bottom tool bar?

I have a navigation controller and I want to set an image to the bottom tool bar and and I want to add buttons to it how would I do this?
Its usually best to read the Documentation a bit as it usually tells you things you to do.
For UIToolbar documentation here
Upon reading this you can see there is a setItems which takes an NSArray of UIBarButtonItems.
Your best bet is probably something along these lines. Assuming you want to be able to Use Images for the buttons. If not, let me know and I can update the answer.
UIToolbar *toolbar = [[UIToolbar alloc] init];
NSMutableArray *arrayOfButtons = [NSMutableArray new];
UIImage *image = [UIImage imageNamed:#"image.png"];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBlack target:self action:#selector(doSomething)];
[array addObject:button1];
[toolbar setItems:arrayOfButtons];

how to display an image in the navigation bar of an iPhone application?

how to display an image in the navigation bar of an iPhone application? (say, right after the title)
Here's how to have the image centered in the NavBar.
UIImage *image = [UIImage imageNamed: #"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple.
The source shows you various ways to manipulate both the StatusBar & NavBar.
I haven't tested this but as UINavigationBar is a view you can add subViews to it.
UIImage* myImage = [UIImage imageNamed:#"Myimage.png"];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;
[myTabbar addSubView:myImageView];
[myImageView release];
You can use things like the backItem property to calculate the position of your image view.
If you want the image at the right of the nav bar, you can define it as a custom button with no action when presed, like this
UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];
Simply Place that code in - (void)viewWillAppear:(BOOL)animated; so it'll work fine
and add one image having size 320x40 named Top Bar
UIImage *image = [UIImage imageNamed: #"TopBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
the navigation bar has a property called title view - set this to the image you like. Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. Still set the title to what you want so it appears on the back button when you push a view Controller
I encountered the same problem.Found out the best solution
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"background_image.png"] forBarMetrics:UIBarMetricsDefault];
Hope this would help....
Just write your own navigation bar. Therefore you have to disable the Navigation Bar fist:
Disable the top bar in the interface builder by selecting your Navigation Controller in
your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None
Afterwards you can add any HeaderView you like...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"header_image.png"]];
self.headerView.backgroundColor = background;
// ...add buttons and labels
[self.view addSubview:headerView];