How to make top navigation controller clickable? - ios

I have the following storyboard. Navigating to Class VC, I need to create custom buttons at the top of the VC and I think it will be beneath the Top Navigation Bar Controller. How do I make my custom buttons on top of the Navigation Bar Controller and clickable. I have already make the Top Navigation Bar appearance to be transparent.
I have the following code that is working and the layout is at the following pic However this is not my desired layout. It is located somewhere below the Transparent Navigation Bar controller area.
//--- customizeable button attributes
CGFloat X_BUFFER = 0.0; //--- the number of pixels on either side of the segment
CGFloat Y_BUFFER = 0.0; //--- number of pixels on top of the segment
CGFloat HEIGHT = 50.0; //--- height of the segment
//--- customizeable selector bar attributes (the black bar under the buttons)
CGFloat SELECTOR_Y_BUFFER = 0.0; //--- the y-value of the bar that shows what page you are on (0 is the top)
CGFloat SELECTOR_HEIGHT = 44.0; //--- thickness of the selector bar
CGFloat X_OFFSET = 8.0; //--- for some reason there's a little bit of a glitchy offset. I'm going to look for a better workaround in the future
CGFloat numControllers = 7.0;
-(void)setupSegmentButtons
{
navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,52)];
navigationView.backgroundColor = [UIColor greenColor];
[self.view addSubview:navigationView];
//——Format Some Dates
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*(self.view.frame.size.width/numControllers), Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];
[navigationView addSubview:button];
NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,0,self.view.frame.size.width/numControllers,30)];
firstLineButton.text = lblDate;
[button addSubview:firstLineButton];
NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,30,self.view.frame.size.width/numControllers,15)];
secondLineButton.text = lblDay;
[button addSubview:secondLineButton];
button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor brownColor];//— buttoncolors
[button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
[self setupSelector];
}
//--- sets up the selection bar under the buttons on the navigation bar
-(void)setupSelector {
selectionBar = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/numControllers, Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];
selectionBar.backgroundColor = [UIColor colorWithRed:189.0f/255.0f green:225.0f/255.0f blue:255.0f/255.0f alpha:0.6];
[navigationView addSubview:selectionBar];
}
I manage to move the custom buttons to the top by changing the Y Coordinates and I got the following results. Its not ideal cause I need the brown colour to be all the way to the edge of the phone especially iPhoneX and the buttons to be clickable.

I was not able to comment that's why suggesting this as an answer.
I think you should present your view controller instead of pushing it.
[self presentViewController:#"your view controller" animated:YES completion:nil];

Answer:
To hide the navigation bar at the current VC or the 1st VC
-(void)viewWillAppear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
To UNHIDE the navigation bar on subsequent VC or the 2nd VC. At the 1st VC, apply the following code.
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
Then the buttons will be clickable.

Related

Changing NavigationItem Title in viewDidAppear in iOS

I've set the NavigationItem.title in Interface builder for readability but in code i need to do
self.navigationItem.title = SomeThingThatComesFromDB
I've Tried to do this in viewDidLoad and viewDidAppear but in first app launching the title is still the thing that I've set in interface builder.
How I can fix this?
This is my root view controller for application launch and here is the code that I'm using:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for (Branch *b in [UserManager sharedInstance].branches) {
if ([b.branchId isEqualToString: [UserManager sharedInstance].vendorId]) {
self.title = b.branchName;
}
}
}
You got the following comparison in place to set/update the navigation item's title.
if ([b.branchId isEqualToString: [UserManager sharedInstance].vendorId]) {
self.title = b.branchName;
}
Problem seems to be that the [UserManager sharedInstance].vendorId] variable is not set (yet), when you call the function to update a navigation item's title. As you are able to set it correctly, after the view had disappeared and appeared back again.
The below code illustrates once again how one can set a navigation item's title, as you also did:
If you want to update the title in general for all related navigation elements (UITabbarController tabs, UINavigationController back button etc.) simply use:
self.navigationItem.title = SomeThingThatComesFromDB
https://developer.apple.com/reference/uikit/uiviewcontroller/1621364-title
If you got a dedicated navigation item in your UINavigationController's bar, you could access it the following way (make sure, there is only one to access)
self.navigationItem.rightBarButtonItem.title = SomeThingThatComesFromDB
self.navigationItem.leftBarButtonItem.title = SomeThingThatComesFromDB
Try out below code:
CGRect rect = self.navigationController.navigationBar.frame;
UIView *myView = [[UIView alloc] init];
UILabel *title = [[UILabel alloc] init];
[title setFont:[UIFont systemFontOfSize:18.0]];
title.text = *SomeThingThatComesFromDB*;
title.textColor = [UIColor whiteColor];
CGSize size = [title.text sizeWithAttributes:
#{NSFontAttributeName:
title.font}];
title.frame = CGRectMake(20, 0, size.width, rect.size.height);//40
myView.frame = CGRectMake(0, 0, 40+size.width, rect.size.height);
[myView addSubview:title];
self.navigationItem.titleView = myView;

navigation bar items not being removed when view disappears

I have a uisegementcontrol and a uibarbutton that gets added to the navigation bar of the navigation controller. When I push to the next view, however, I want them to be removed. I tried several approaches, but nothing seems to be removing them. One approach was using viewWillDisappear and having the segementcontrol and the bar button removedFromSuperView. Another was to call the removeFromSuperView from the method that pushes to the next scene. Nothing seems to be working. I've actually been stuck on this for a really long time!
Also, the view controller "X" that has the segement control and the bar button is presented through a presentViewController. X then pushes the other view controller.
Here is the code for the segement control
NSArray *headers = #[#"Tracks", #"Playlists"];
self.savedMusic = [[UISegmentedControl alloc] initWithItems:headers];
CGSize size = self.view.frame.size;
CGRect segFrame = CGRectMake(80, 5, size.width - 140, 35);
self.savedMusic.backgroundColor = [UIColor blackColor];
self.savedMusic.frame = segFrame;
self.savedMusic.tag =1;
[self.savedMusic setTitle:#"Tracks"
forSegmentAtIndex:0];
[self.savedMusic setTitle:#"Playlists"
forSegmentAtIndex:1];
[self.savedMusic addTarget:self
action:#selector(whatToDisplay)
forControlEvents:UIControlEventValueChanged];
self.savedMusic.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:self.savedMusic];
Here is the code for the viewWillDisapear
[super viewWillDisappear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.navigationItem.leftBarButtonItem = nil;
[self.savedMusic removeFromSuperview];
And here is the code for the method that switches to the new view
PlaylistTracksTableViewController *vc = [[PlaylistTracksTableViewController alloc] init];
[self.savedMusic removeFromSuperview];
[[self.navigationController.navigationBar viewWithTag:1] removeFromSuperview];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationController pushViewController:vc animated:YES];
The problem is that what you are doing is not how you manipulate the contents of the navigation bar. Do not talk to self.navigationController.navigationBar. Instead, each view controller should set its own self.navigationItem, and the runtime will use that to populate the navigation bar for that view controller.

change the color of scrollbar in tableview

I have one viewcontroller.Inside that i placed one tableview with size 320X295 at centre of my viewcontroller. so my table view will be half size at centre in my viewcontroller.
My tableview is not fully fill my Viewcontroller.Now how can i change my scroll bar color to green color or some RGB Value.
Here is my code:
UIScrollView *scView = [[UIScrollView alloc] init];
scView.frame = self.view.bounds; //scroll view occupies full parent views
scView.contentSize = CGSizeMake(400, 400);
// scView.backgroundColor = [UIColor lightGrayColor];
scView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scView.showsHorizontalScrollIndicator = NO;
scView.showsVerticalScrollIndicator = YES;
scView.scrollEnabled = YES;
[self.containerTableView addSubview: scView];
ContainerTableView is my tableview name.
i need to change my scroll bar color to green.Please help me how can i achive that.Thanks
1 .Add UIScrollViewDelegate
#interface ViewController : UIViewController<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
#end
2. Add scrollViewDidScroll in implementation section
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//get refrence of vertical indicator
UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
//set color to vertical indicator
[verticalIndicator setBackgroundColor:[UIColor redColor]]; // Your color
//get refrence of horizontal indicator
UIImageView *horizontalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-2)]);
//set color to horizontal indicator
[horizontalIndicator setBackgroundColor:[UIColor blueColor]]; // Your color
}
You can achieve this by using the property of UIScrollView named indicatorStyle
Use any of the following
typedef enum {
UIScrollViewIndicatorStyleDefault,
UIScrollViewIndicatorStyleBlack,
UIScrollViewIndicatorStyleWhite
} UIScrollViewIndicatorStyle;
you can use this style like this.
tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
here is the link for reference..
Change width and colour of scroll bar in UITableView, iphone

Adding sliding subview in a frame using TabBar button of a View controller

I have a Tab-Bar button on a View Controller.I want when a user press that button a sub-view sides from the corner and shows the list of buttons for specified actions and on pressing tab bar button again sub-view should slide back and disappear.This sub-view should be shown on small frame of main View. How i can accomplish this task?
Add a custom action to that button.
when a user presses the button check to see if the buttons view's origin is within the bounds of your main view. if it's not, slide it in, else slide it out.
I would probably do something like that:
// Call this one on viewDidLoad
-(void)createMenuButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = item;
}
// Make sure the menuView property was added as subview of self.view
-(void)toggleMenu {
if (CGRectContainsPoint(self.view.bounds, self.menuView.frame.origin)) {
[UIView animateWithDuration:.35
animations:^{
CGRect rect = self.menuView.frame;
rect.origin.x -= self.menuView.frame.size.width;
self.menuView.frame = rect;
}];
} else {
[UIView animateWithDuration:.35
animations:^{
CGRect rect = self.menuView.frame;
rect.origin.x += self.menuView.frame.size.width;
self.menuView.frame = rect;
}];
}
}
Hope this helps :)

Adding a UISegmentedControl under NavigationBar

I’m adding a UISegmentedControl right under the NavigationBar in a UITableViewController. This is the code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationBar = self.navigationController.navigationBar;
UIView *segmentView=[[UIView alloc] initWithFrame:CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 50)];
[segmentView setBackgroundColor:[UIColor whiteColor]];
segmentView.alpha = 0.95;
self.tabSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Favourites", #"All", nil]];
self.tabSegmentedControl.frame = CGRectMake(20, 10, self.navigationBar.frame.size.width - 40, 30);
[self.tabSegmentedControl addTarget:self action:#selector(tabChanged:) forControlEvents:UIControlEventValueChanged];
[segmentView addSubview:self.tabSegmentedControl];
[self.navigationBar addSubview:segmentView];
[self.tabSegmentedControl setSelectedSegmentIndex:1];
}
The view and the SegmentedControl appear on the screen well, but they are not clickable. The selector doesn’t get executed when tapped on the SegmentControl; it doesn’t even switch tabs! In fact, the stuff that is underneath the segmentView (items in the TableView) get clicked when you tap on it. I have tried but failed to understand why this is happening! Any suggestions would be helpful!
You are adding a view below the bounds of its super view. You may see the view however you cannot click it because it is out of bounds. If you set the property of the navigation bar clipsToBounds to YES you should see that the view disappears. What you need to do is add the segment controller to the table view. Here is an example:
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.view addSubview: self.segmentView]; // need to keep a pointer to segmentView
self.tableView.contentInset = UIEdgeInset(self.segmentView.frame.size.height, 0,0,0);
}
-(void) scrollViewDidScroll:(UIScrollView*) scrollView{
CGRect rect = self.segmentView.frame;
rect.origin = self.tableView.contentOffset;
self.segmentView.frame = rect;
}

Resources