iOS-Segmented control in navigation bar - ios

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

Related

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.

UIViews moving after segue and return

I have an app in which I am creating a two views stacked on top of each other:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height -200)];
[self.view addSubview:headerView];
[self.view addSubview:bottomView];
[headerView setBackgroundColor:[UIColor lightGrayColor]];
[bottomView setBackgroundColor:[UIColor redColor]];
I also use a sidemenu which uses:
[self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"welcomeScreen"]]
animated:YES];
[self.sideMenuViewController hideMenuViewController];
The initial load is fine and both top and bottom views appear as expected, however, when I navigate away using the side menu and then navigate back to home using the sidemenu, both views appear to move up the page (behind the navigation bar).
I have tried everything I can think from programmatic constraints to checking that the Navigation bar is present and moving the code from viewDidLoad to viewDidAppear but I always get the same result. Any ideas as to what is happening and why? And any suggestions on a fix would be greatly appreciated. Thanks
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Has fixed the issue!

ios to create view programmatically

I am trying to create a view like the below one programmatically but it covers the whole screen and does not be like this. There could be some thing size = Freeform, but could not find code on google to handle this.
UIView *picker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
picker.backgroundColor = [UIColor whiteColor];
Actually I want to create this whole view programmatically not though interface Builder.
please help
No other way than CGRectMake
if you want create the UIView that covers the whole screen
UIView *picker = [[UIView alloc] initWithFrame:self.view.bounds];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
if you want create the UIView that not covers the whole screen
UIView *picker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 260)];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
Create UIToolBar which has UIBarButtonItem(=Done).
Set the UIToolBar as UIDatePicker's inputAccessoryView.
You don't even need a backgroundView to add Done button and date picker.

My UIView has a UIButton as one of it's subviews but not responding to events in objective-c

Here is my code. It has started to look crowded after an hour or 2 of trying to solve this issue. I've tried setting user interaction enabled to yes, I've tried just using a button alone and not making it the subview of another view.
Right now I have filterBar > filterBarContainer > filterButton.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
// Create filter bar with specified dimensions
UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[filterBar setUserInteractionEnabled:YES];
// Make it a subview of navigation controller
[[[self navigationController] navigationBar] addSubview:filterBar];
[filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
[filterBar setTag:1];
// Reusable vars
// CGFloat filterBarX = filterBar.frame.origin.x;
//CGFloat filterBarY = filterBar.frame.origin.y;
CGFloat filterBarHeight = filterBar.frame.size.height;
CGFloat filterBarWidth = filterBar.frame.size.width;
// Create centre filter button with specified dimensions
UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
[filterButtonContainer setUserInteractionEnabled:YES];
// Make it a subview of filter bar
[filterBar addSubview:filterButtonContainer];
[filterButtonContainer setBackgroundColor:[UIColor redColor]];
// Centre the button
[filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];
// Add button to filter button
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[filterButton setUserInteractionEnabled: YES];
[filterButton setFrame:CGRectMake(0, 0,40 , 20)];
[filterButton setTitle:#"test" forState:UIControlStateNormal];
[filterButtonContainer addSubview:filterButton];
[filterButton setBackgroundColor:[UIColor yellowColor]];
// self.filterButton = filterButton;
[filterButton addTarget:self action:#selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"dd");
filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
filterButtonContainer.layer.borderWidth = 1;
This is the method I'm trying to trigger.
- (void)filterButtonTapped:(UIButton *)sender
{
NSLog(#"filter button tapped");
UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:#"Filter Garments"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Recommended", #"What's New", #"Price - High to Low", #"Price - Low to High", nil];
[filterOptions showInView:self.collectionView];
}
Most likely one of your parent views (superviews of your button) have a width or height that places the filter button outside of their area. The button is still displayed (if you don't define that the view should cut off subviews), but elements outside the view's area will not get any touch events. Check your superview's width and height.
You're adding filterBar as a subview of your navigationBar, but you're setting it's y-origin within navigationBar to 42. And then you're setting your filterButtonContainer's y-origin within the filterBar to 2. Assuming navigationBar's height is 44, most of the filterBar and the entirety of your filterButtonContainer and filterButton won't be within their navigationBar superview so the button wouldn't be selectable.
Edit: To make it selectable while keeping the positioning intact, I suggest adding the filter bar to self.view. For example, change:
[[[self navigationController] navigationBar] addSubview:filterBar];
to
[self.view addSubview:filterBar];

Embedding a CGRect in Navigation Bar

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;

Resources