I have 2 ViewController and first one is rootviewcontroller for NavigationController. I want to remove background of navigationbar in second viewcontroller and just show my navigationitem at top.
How to do that ? and what I should to do when I pop second viewcontroller, navigationbar have its old background not the secendview controller background ?
here is my code :
#implementation testView1
- (void)viewDidLoad
{
[super viewDidLoad];
[button addTarget:self action:#selector(ClickAction) forControlEvents:UIControlEventTouchUpInside ];
self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationItem.title = #"Home";
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor]};
}
-(void)ClickAction
{
testView2 *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"V2"];
[self.navigationController pushViewController:detail animated:YES];
}
#implementation testView2
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
self.title = #"No";
self.navigationController.navigationBar.barTintColor = nil;
self.navigationController.navigationBar.backgroundColor = nil;
self.navigationController.navigationBar.tintColor = nil;
self.navigationController.view.tintColor = nil;
self.navigationController.view.backgroundColor = nil;
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"left-arrow.png"] style:UIBarButtonItemStylePlain target:self action:#selector(Back:)];
backButtonItem.imageInsets = UIEdgeInsetsMake(45, 5, 45, 85);
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) Back:(id) sender
{
[self.navigationController popViewControllerAnimated:YES];
}
In firstcontroller
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationItem.title = #"Home";
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor]};
[super viewWillAppear:animated];
}
and write code in SecondView controller
- (void)viewDidLoad
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
}
ViewController.h file:
- (void)showForViewController:(UIViewController*)controller;
ViewController.m file:
- (void)showForViewController:(UIViewController *)controller {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0"))
{
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
}
else
{
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
}
[controller.navigationController presentViewController:self animated:NO completion:nil];
}
I'm using this code for present ViewController with clear background (to see previous controller).
Related
I need something like this status bar image but i am getting the status hidden but still i can not change the color. I am getting this status bar withour color
This is what i am doing.
-
(void)showGallery
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = NO;
// picker.navigationBarHidden = YES;
// picker.toolbarHidden = YES;
picker.navigationController.navigationBar.barTintColor=[UIColor orangeColor];
picker.navigationBar.backgroundColor = [UIColor orangeColor];
picker.navigationBar.barStyle=UIBarStyleBlack;
[ picker.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[self presentViewController:picker animated:YES completion:NULL];
}
Use three steps:
1: Add UINavigationControllerDelegate, UIImagePickerControllerDelegate to your #interface yourController ()<>
2:
imagePickerController.delegate = self;
3:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
Try This:-
1.Create a view with height 20 points and change the background color to attach the viewcontrollerView (OR) change the background colour of your view controller follow like
UIView*statusbarview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
statusbarview.backgroundColor = [UIColor greenColor];
[self.view addSubview:statusbarview];
(OR)
self.view.backgroundColor = [UIColor redColor];
This solved my problem.
-(void)showGallery
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = NO;
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.navigationBar.translucent = NO;
picker.navigationBar.barTintColor = [UIColor orangeColor];
picker.navigationBar.tintColor = [UIColor whiteColor];
picker.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
[self presentViewController:picker animated:YES completion:nil];
}
i am new in objective-c .I am trying to hide navigationbar i used this code and it's work perfectly but problem is this when i hide navigationbar after i am failed to show navigationItem's rightBarButtonItem
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
// self.navigationItem.rightBarButtonItem.enabled = NO;
}
i am used this code for navigationItem
-(void)loadBackButton{
/*UIBarButtonSystemItemDone */
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss:)];
self.navigationItem.rightBarButtonItem = buttonItem;
}
- (void)dismiss:(id)sender {
NSLog(#"Dismis Done");
[self dismissModalViewControllerAnimated:YES];
//[self.view removeFromSuperview];
}
and my - (void)viewDidLoad are bellow
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//for load navigationItem's
[self loadBackButton];
self.navigationItem.rightBarButtonItem.enabled = YES;
// Do any additional setup after loading the view from its nib.
}
Hiding NavigationBar will hide your rightBarButtonItem too. One of the possible solutions is that you make the Navigation Bar transparent. You can use following code to do so:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
There is a lot of questions about how to replace the default back button with a custom back button, but none that I know of about how to replace a custom back button with the default.
I have a web view that when [self.webView canGoBack] == YES, a custom back button appears. But after you go back all the way, that custom back button is still there, rather than the default. Is there something I can do to replace my custom back button with the default when [self.webView canGoBack] == NO?
Here is my relevant code:
#interface MerchViewController () <UIWebViewDelegate>
#property UIWebView *webView;
#end
#implementation MerchViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = #"Merchandise";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
self.view = webView;
self.webView = webView;
webView.delegate = self;
[self setURL];
}
- (void)updateBackButton {
if ([self.webView canGoBack]) {
{
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Back" tintColor:[UIColor orangeColor] target:self andAction:#selector(backWasClicked:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
}
}
else {
/*
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Fan Zone" tintColor:[UIColor orangeColor] target:self andAction:#selector(openMenu:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
*/
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
[_spinner stopAnimating];
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
- (void)setURL
{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.lawlorscustom.com/omaha-lancers-hockey"]];
NSLog(#"loadRequest: %#", req);
[(UIWebView *)self.view loadRequest:req];
}
#end
The "else" portion in "updateBackButton" is commented out because if I don't comment it out, when the view first loads there are two back buttons overlapping.
After struggling for a while, the answer was to do:
[self.navigationItem setHidesBackButton:NO animated:NO];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
I use native UIImagePickerController in of my view controller.
I use a different color for UIBarButtonItem than what iPhone uses UIImagePickerController
is it possible for me to change textColor of UIImagePickerController.UIBarButtonItem ??
I tried
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
for (UIBarButtonItem *item in picker.navigationItem.rightBarButtonItems) {
item.tintColor = [UIColor redColor];
}
for (UIView *view in picker.navigationBar.subviews) {
NSLog(#"%#", [[view class] description]);
if ([[[view class] description] isEqualToString:#"UIBarButtonItem"]) {
[(UIBarButtonItem *)view setTintColor:[UIColor redColor]];
}
}
[self presentViewController:picker animated:YES completion:NULL];
Output of NSlog was
_UINavigationBarBackground
_UINavigationBarBackIndicatorView
EDIT 2:
I needed to do
picker.navigationBar.tintColor = [UIColor redColor];
I have a BarButtonItem and a Popovercontroller. the question is who can i pop something when i click on the button can some one help me. i will post some code what i have on this moment.
-(void) showPop:(id)sender{
NSLog(#"test");
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:popover];
[pop setDelegate:self];
[pop presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//self.title = NSLocalizedString(#"Nieuws", #"Nieuws");
/*if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}*/
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_loading = NO;
self.title = #"Agenda";
if (_refreshHeaderView == nil) {
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
}
// update the last update date
self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
}
[_refreshHeaderView refreshLastUpdatedDate];
self.tableView.contentInset = UIEdgeInsetsMake(66.0f, 0.0f, 0.0f, 0.0f);
[_refreshHeaderView egoRefreshScrollViewDidEndDragging: self.tableView];
//[self loadData];
//[popButton addTarget:self action:#selector(showPop:) forControlEvents:UIControlEventTouchUpInside];
button = [[UIBarButtonItem alloc] initWithTitle:#"Filter" style:UIBarButtonItemStyleBordered target:self action:#selector(showPop)];
self.navigationItem.leftBarButtonItem = button;
}
Might just be a typo but when you create your button, the selector should be showPop: (with a colon) not showPop.