Embedding a CGRect in Navigation Bar - ios

Is it possible to embed a CGRect into a navigation bar just like this screenshot: here
I've done this:
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
[self.view addSubview:aView];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(100, -15, 150, 10)];
Using a negative figure, it actually allows me to place the label in the same space as the nav bar, but it will be hidden behind it. Is there way for me to bring the label to the front (change the z-index?)? Or is there a way to embed a label in the nav bar?
I've tried using this code but it doesn't effect the label:
[self.view bringSubviewToFront:aView];
Answered thanks to EmptyStack:
Adding this code:
self.navigationItem.titleView = aView;
[aView addSubview:title];
Allows to embed the title (or more) in the navigation bar.

I guess you want this one.
self.navigationItem.titleView = aView;

Related

Is it possible to dim a UIView but allow touch of elements behind it?

How would you have a grey overlay on top, but allow to touch buttons beneath it?
You can set userInteraction of overlay to false.
As mentioned, you can set userInteractionEnabled to NO.
But pay attention: when you create a full screen transparent view, and set its userInteractionEnabled to NO, all the subviews of that view will not be responsive to user actions. If you add a button on your view, it will not respond to user taps! and another problem: if you set the alpha value of that view to be transparent, e.g. 0.4, then all its subviews will be transparent too!
The solution is simple: don't put any subview on your transparent view, but instead, add your other elements as siblings of your view. Here is Objective-C code to show what I mean: (notice the comments which say it all):
//Create a full screen transparent view:
UIView *vw = [[UIView alloc] initWithFrame:self.view.bounds];
vw.backgroundColor = [UIColor greenColor];
vw.alpha = 0.4;
vw.userInteractionEnabled = NO;
//Create a button to appear on top of the transparent view:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 80, 44)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn setTitle:#"Pressed" forState:UIControlStateHighlighted];
//(*) Bad idea: [vw addSubview:btn];
//(**) Correct way to have the button respond to user interaction:
// Add it as a sibling of the full screen view
[self.view addSubview:vw];
[self.view addSubview:btn];

UISearchBar Layout Issues (iOS 8)

I'm making what should be a simple change to an app to add a UISearchBar component, but am getting some fairly strange layout quirks in the UI. It's perhaps easier to illustrate using pictures.
Here's what the component looks like when it doesn't have focus:
Note that the icon and the placeholder text are too far over, and the placeholder text is too far up. And here's what happens when I tap on the field:
The icon is now in the correct place, but the placeholder text is now too far to the left and overlapping with the icon. What gives?
When the field loses focus, it reverts to its original layout. Unless I type some text into it first, in which case it retains the second layout.
The code I'm using to set up the UISearchBar isn't doing anything special:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
containerView.backgroundColor = UI_COLOR_NAV_TITLEBAR;
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width - 40, 44)];
searchField.placeholder = #"search all items";
searchField.searchBarStyle = UISearchBarStyleMinimal;
searchField.showsBookmarkButton = NO;
searchField.showsCancelButton = NO;
searchField.showsScopeBar = NO;
searchField.showsSearchResultsButton = NO;
UITextField* textSearchField = [searchField valueForKey:#"_searchField"];
searchField.backgroundColor = [UIColor clearColor];
textSearchField.textColor = UI_COLOR_NAV_TEXT;
searchField.delegate = self;
[containerView addSubview:searchField];
//...
I want the UISearchBar to always appear as in the second example, except obviously without the text field overlapping with the icon. Is there a simple solution to this issue that I've overlooked?
Try following code for searchField..
[searchField setLeftViewMode:UITextFieldViewModeNever];
[searchField setRightViewMode:UITextFieldViewModeNever];
[searchField setBackground:[UIImage imageNamed:#"searchicon.png"]];
Last is to show icon at left side, use image for that.

Add a sticky view underneath the Navigation Bar

I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.
I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.
Here's what worked:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_dateDisplay setText:#""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:#""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_resultsDisplay setText:#""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?
If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.

iOS-Segmented control in navigation bar

I'm looking for a way to add a segmented control to my navigation bar, but I still want the title and bar buttons to be there.
Like the purchased section in the app store:
I have tried adding a bar segmented control to my navigation item, then using the prompt instead of the title, but the prompt does not have bold text. Could I make the text bold and still have bar buttons too?
I know you probably found another way, and that my answer may sound cheap, but here's what I did. I just added a view underneath my navigation bar with the proper constraints, and put my segmented control there. It works good for my application and looks pretty much like that picture.
Have a nice day.
give you the dirty example I made
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, 60)];
view.backgroundColor = UIColorFromRGB(0xffffff);
[self.view addSubview:view];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 124, 320, 1)];
bottomView.backgroundColor = [UIColor grayColor];
[self.view addSubview:bottomView];
UISegmentedControl *sg = [[UISegmentedControl alloc] initWithItems:#[#"one", #"two"]];
sg.frame = CGRectMake(10, 10, 300, 40);
[view addSubview:sg];
for (UIView *view in self.navigationController.navigationBar.subviews) {
for (UIView *subView in view.subviews) {
[subView isKindOfClass:[UIImageView class]];
subView.hidden = YES;
}
}
the screenshot
hope this can enlighten you to achieve what you want

Add UISlider To UINavigationBar

Trying to add a UISlider to UINavigationBar. I have it to where the slider shows, but when trying to change the value, it will not move. Here is the code in viewWillAppear. Note that this is on a child view and not the main parent view.
UIView *viewofslider = [[UIView alloc] initWithFrame:CGRectMake(160, 10, 40, 30)];
UISlider *theslider = [[UISlider alloc] init];
[theslider addTarget:self action:#selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[theslider setBackgroundColor:[UIColor clearColor]];
theslider.minimumValue = 50.0;
theslider.maximumValue = 150.0;
theslider.continuous = YES;
theslider.value = 100.0;
[viewofslider addSubview:theslider];
[self.navigationController.navigationBar addSubview:viewofslider];
To answer your specific question, I think the width of viewofslider is too narrow at 40.0, so the touch is not being picked up by the UISlider.
If you change the width of viewofslider to 100.0 you should be able to interact with it.
However, I agree with rmaddy's comment that you should simply add the slider to the navigation bar, there's no obvious need for the UIView.
Try to add it to titleView of view controller's navigation item in viewWillAppear:
UISlider *theslider = [[UISlider alloc] init];
...
self.navigationItem.titleView = theslider;

Resources