I set a default background image for UISearchBar:
[[UISearchBar appearance]setSearchFieldBackgroundImage:[UIImage imageNamed:#"search_field"] forState:UIControlStateNormal];
I then have a UISearchBar set to the nav bar's titleView.
UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 35)];
lsearchBar = [[TestSearchBar alloc]initWithFrame:searchView.frame];
[searchView setBackgroundColor:[UIColor clearColor]];
[searchView addSubview:lsearchBar];
But the result looks horrible with a crushed graphic for my search bar:
If it try to embed it without using UIView directly, like this:
lessonsSearchBar = [[TestSearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
self.navigationItem.titleView = lessonsSearchBar;
it looks better, but when I push segue to the next view controller, or do pop back with animation it tries to animate it and looks horrible with this gray bar around it:
I do try it inside UIView because I also want to set a custom size to my UISearchBar and realign it a bit to the left side of the screen, which for some reason is not allowed when I set my search bar directly to navitationItem.titleView. I'm not sure exactly how to achieve this without crashing my image (which I set with setSearchFieldBackgroundImage) or without those glitches with animation. Can you please help?
Related
I added a UISegmentedControl programmatically to navigationItem.titleView
The first time it is displayed it is centered like I would expect. But as soon as I dive down into a detail view controller, I can see my titleView's view pushed into the upper left corner, and nothing I've tried gets it back
In my initialization method I do this
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl setTranslatesAutoresizingMaskIntoConstraints:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//segmentedControl.frame = CGRectMake(0, 0, 280.0f, 30.0f);
self.navigationItem.titleView = segmentedControl;
I realized after a while that setting the CGRectMake has no affect
I've also tried putting this login in viewDidAppear but also did not change this outcome
Previously, when I tried putting a UISegmentedControl inside of another UIView, I lost the ability to click on the UISegmentedControl (I'm not sure if I had a variation of this which solved my positioning problems though)
If you only have 2 segments I don't think it's needed for the resizing, its width will always fit even for iPhone 4 and lower.
What happens if you remove the following:
[segmentedControl setTranslatesAutoresizingMaskIntoConstraints:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
I am trying to customise the navigation bar with title view.
But it seems setting title view comes with its own left and right and top paddings.I was expecting the title view to cover the whole navigation bar according to the frames given.
Is it expected behaviour and if YES than how to deal with that?
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
view.backgroundColor = [UIColor greenColor];
//Navigation Bar
self.navigationItem.titleView = view;
If you just want the navigation bar to be green use [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; in iOS 7+ or [self.navigationController.navigationBar setTintColor:[UIColor greenColor]]; in iOS 6-
Yes, it seems it is not possible to remove that left/right padding. This is the screenshot form debugging views in Xcode after running your code
The outside view that is gray is the navigationBar and the green is obviously the titleView. No matter the frame for the titleView, it is clipped.
Apple documentation says just this:
Custom title views are centered on the navigation bar and may be resized to fit.
I think the only solution would be to subclass the navigationBar, so that you override the titleView frame.
I am creating a UINavigationBar programmatically, which is displaying as intended in iOS 7. However, in iOS 8.x all the text in the UINavigationBar doesn't appear within the bounds of the screen. One of my assumptions is this has something to do with Auto Layout.
//ViewControllerRootHomeCenter.m
_navBar = [[UINavigationBar alloc] init];
_navBar.frame = CGRectMake(0,0, self.view.frame.size.width, 64);
// add hambuger menu
//...etc etc
[self.view addSubView:_navbar];
You can try to obtain the screen width this way too:
CGRect frame = (CGRect){0,0,CGRectGetWidth([[UIScreen mainScreen]bounds]),64);
Perhaps it will fix your problem.
First of all, make sure you're not hard-coding the width of the navigation bar.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
Secondly, if your auto layout is enabled, make sure there are no conflicting constraints. If it's not enabled, then I don't see why you should be having this problem.
I´v read the posts regarding this issue but found nothing to work, I think it must be since I am using a toolbar and have the View Controller embedded in a container view. The status bar always overlaps. No matter if I try to replace the toolbar or set different settings in IB. It also seems that since it´s inside a container view, setting the inferred option does not work. I even tried putting two toolbars on top, but only this one shows.
The status bar in iOS 7 is transparent, so if you want to have a look similar to that of iOS6, create a view of 20 pixels and add it inside your "container" view...
UIView *statusBarView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 20)];
statusBarView.backgroundColor = [UIColor blackColor];
[containerViewController.view addSubview:statusBarView];
Then set the frame of your "contained" ViewController accordingly
containedViewController.frame = CGRectMake(0, 20, 320, itsCurrentHeight - 20);
This way the status bar should not overlap with your content anymore
I'm writing an iPhone app with a table inside a navigation controller. When the user clicks one of the cells in the main screen a UIView on top of the incoming view controller is created (it's like a toolbar).
self.toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
toolbar.backgroundColor = [UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1.0];
[self.navigationController.view addSubview:toolbar];
The problem is that the view appears before the transition to the new view controller is complete and the effect is pretty weird. I suppose this is due to the fact I add the view to the navigationController,but I need to do this otherwise the bar would scroll together with the table and instead I want it to be fixed.
Any suggestion?
I've found a possible solution: add the toolbar as TableHeaderView and follow iOS: Add UIView to UITableView
Any other better solution is more than welcome