BarButtonItem on NavigationBar is forced to be transparent if returned with popViewController - ios

I have a problem about UIBarButtonItem on NavigationBar.
Transit from FirstViewController to SeconderViewController by pusuViewController.
Return to FirstViewController with return button on left side of NavigationBar.
[Problem happen] The Color of [next] button on right side of NavigationBar is transparent.
(You can tap [next] button althought the color is transparent )
This problem happen on iPhone8(iOS11.2.1(15C153), not heppen on iPhone6(iOS10.3.3(14G60)).
My code is below,
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.backgroundColor = UIColor.blackColor;
UIViewController *vc = [[FirstViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewContrtoller.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"First View";
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"next", nil)
style:UIBarButtonItemStylePlain
target:self
action:#selector(touchUpNextButton:)];
self.navigationItem.rightBarButtonItem = nextButton;
}
- (void)touchUpNextButton:(id)sender
{
UIViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = #"Second View";
}
I'd appreciate if you would provide me a good solution.
Thank you.

I think it is the bugs of iOS 11.2.1
You can temporary fix by following solution:
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"next", nil)
style:UIBarButtonItemStylePlain
target:self
action:#selector(touchUpNextButton:)];
[nextButton setTitleTextAttributes:#{NSForegroundColorAttributeName : [self.view tintColor], NSFontAttributeName:[UIFont systemFontOfSize:16.9f]} forState:UIControlStateNormal];
Hope that can help you.

Related

MMDrawerController with UITabBarController showing a black gap on top of the center view

I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger

Can't pop iOS viewController. Not sure, but I think it's something with the Navigation Controller

I'm having trouble trying to pop a view
App Delegate
#implementation MAAppDelegate
#synthesize navController;
#synthesize detailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Init the navController for the Master Detail View of the grade cells
UINavigationController *navController = [[UINavigationController alloc] init];
detailViewController = [[UIViewController alloc] init]; //step6
navController = [[UINavigationController alloc] initWithRootViewController:[[MAController alloc] init]]; //step7
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navController; //step8
[self.window makeKeyAndVisible];
// Set MAController as rootViewController
//self.window.rootViewController = [[MAController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Use the insanely cool TSMessages to show network alerts
[TSMessage setDefaultViewController: self.window.rootViewController];
return YES;
}
First part of viewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
Later, when I change the viewController
NSLog(#"Opened progress report");
UIViewController *detailViewControl = [[UIViewController alloc] init];
// Set progress report as the view controller
[self.navigationController pushViewController:detailViewControl animated:YES];
UIImage *background = [UIImage imageNamed:#"bg"];
// Add static image bg
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
// Add blurred layer to image when tableView goes in front of it
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
[self.navigationController setNavigationBarHidden:NO];
So I don't understand why that when I do this, a selector from the button (that I know fires, because I get Righthtere in my log):
-(void)home:(UIBarButtonItem *)sender {
NSLog(#"Righthtere");
// Set progress report as the view controller
[self.navigationController popToViewController:self animated:YES];
}
It doesn't go back to the initial view controller.
You seem to be confusing popToViewController and popViewControllerAnimated. popViewControllerAnimated removes the current view from the stack and brings the new stack top the active view controller. popToViewController pops the stack until the listed view controller is on top of the stack.
Since you are calling popToViewController with self, it will look and see that the requested view controller is already on top of the stack and do nothing. If you wish to go back one view controller then your call should be.
[self.navigationController popViewControllerAnimated:YES];
I use the below code to pop the previous viewcontroller in iOS 8.
[self presentModalViewController:viewcontroller animated:YES];

How to create a UINavigationBar programmatically

I've recently started learning Obj-C and according to my friend (who is experienced with Obj-C), it's better if I learnt how to create a view programmatically and not rely on StoryBoards.
So... I've been trying to create a NavigationBar with a left button on it. But, when I run it, I get the actual NavigationBar, but not the button. Here's my code:
The AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
firstController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
and here's the MainViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *navbarColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = navbarColor;
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:NULL];
self.navigationController.navigationItem.leftBarButtonItem = button;
}
Any help would be appreciated. I'm fairly new to Objective-C but I understand the code concept, so you don't need to "baby" talk it when explaining ^.^
add this line before [self.window makeKeyAndVisible];
[self.window addSubview:navController.view];

Why won't this UIToolBar appear?

Here is the init method of the root view controller of my navigation controller that I am trying to display the toolbar on:
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nil bundle:nil];
if(self){
//GUI implementation
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:nil];
self.toolbarItems = [NSArray arrayWithObjects:flexiableItem, item1, nil];
UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addEmployee)];
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
and here is my delegate application:DidFinishLaunching method in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController* navbar = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.window setRootViewController:navbar];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The toolbar is not showing up at all. Could someone point out where I am doing something wrong? Much appreciated.
Move the initializing of your toolbarItems to the initializer you're actually calling ( initWithTableViewStyle: ) and self.navigationController.toolbarHidden = NO; to viewWillAppear as self.navigationController will be nil in your intializer.
Because you are not calling it!
You are calling
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];
where you should be calling
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithNibName:<"Some Name"> bundle:<"Some Bundle">];
As far as I can see, initWithNibName is no called. Yoc call initWithStlye.
And even then - at that point in time your hvc is not part of a navigation hierarchy. Meaning self.navigationController should be nil during init. It gets value as soon as it is assigned as rootviewcontroller to the later newly created UINavigationController.

UIViewController presented modally show up shifted to the top

I'm presenting a UIViewController with presentModalViewController:animated.
CMImportViewControlleriPhone *import = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[import setModalPresentationStyle:UIModalPresentationFormSheet];
[import setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:import animated:YES];
[import release];
However the top bar is not visible, and it is seems shifter to the top (there is an empty space on the bottom).
This is viewDidLoad in which I set the Close button on the navigationItem
- (void)viewDidLoad
{
[super viewDidLoad];
closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(closeButtonPushed:)];
[[self navigationItem] setRightBarButtonItem:closeButton];
[closeButton release];
}
thanks
You should add a navigation bar and then present modalView
CMImportViewControlleriPhone *obj = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[obj setDelegate:self];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:obj];
[self presentModalViewController:navigationController animated:YES];
[obj release];
[navigationController release];
hope this helps. happy coding :)
If you're working with iPhone remove
[import setModalPresentationStyle:UIModalPresentationFormSheet];
When you add a UIBarButtonItem, the NavigationController is nil, and navigationBar is nil also. So it doesn't work with navigationItem.
closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(closeButtonPushed:)];
[[self navigationItem] setRightBarButtonItem:closeButton];
You should add a NavigationController for the import object, and present it.
CMImportViewControlleriPhone *import = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[import setModalPresentationStyle:UIModalPresentationFormSheet];
[import setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:import];
[self presentModalViewController:import animated:YES];
[import release];
[nc release];

Resources