iOS ,I can't finish the background color translucent with UIViewcontroller and UIWindow? - ios

Why the background color of UIWindow is black which is not my destination,and I want to make the background translucent

How can your deepest window be transparent.. What would you see? The home screen?
If you set your background to clearColor, it will just appear black. Try setting it to white and then make your view's have some sort of alpha below 1.0 to make it translucent. If I'm understanding your question properly.

If I am guessing right you want to display the previous view in background instead of black color. If this is the case you need to set these 2 properties on the view controller you are displaying :-
yourpresentingVC.view.backgroundColor = [UIColor clearColor];
yourpresentingVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:tutorial animated:YES completion:NULL];

Related

Grey color background on pushing transparent background UIViewController inside a Container View (Storyboard)

I'm pushing a UIViewController (A) with clearColor as background in a UINavigationController (N).
N is placed inside a Container View. This view has a background that I want to show always.
Then, when Push is animated it shows a grey/transparent background in my VC (A) and when it finishes its animation, it shows correctly.
Is there any way to avoid this grey color?
I have 'done' it by putting a white view background in A and making it disappear in viewDidAppear but I think it can be done without that trick...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
__weak TestsViewController *weakSelf = self;
[UIView animateWithDuration:.3 animations:^{
TestsViewController *ownSelf = weakSelf;
ownSelf.backgroundView.alpha = 0;
}];
}
Set your app's window's background colour to white:
// applicationDidFinishLaunching
self.window.backgroundColor = [UIColor whiteColor];
The push animation often ends up using the background colour of the window behind the transparent areas, and the window background colour is clear by default, so it shows black through the transparent navigation bars and toolbars.
EDIT: Sorry, misunderstood what the issue was. When you push a view controller with a transparent background colour, the background you see behind the view controller is the shadow that is rendered behind your view controller (you can see the edge of it when in a non-transparent view controller).
This question has an answer to your issue. Basically, you have to implement a custom animation controller, to provide the transition animation yourself (as described here). It would probably be easier to continue using your trick than implementing your own custom navigation transition.

iOS7/8 translucent navigationBar top right corner of the black

iOS8/iphone
When setting
navigationbar.translucent = yes;
If also set
blueVC.hidesBottomBarWhenPushed = yes;
push ViewController, navigationbar right corner displays a black.
Test Results:
Black areas are UIWindow by default. I changed the UIWindow background color to white and that fixed the issue. But this just looks better, does not completely resolved.
=======================
iOS7 strange animation when using hidesBottomBarWhenPushed
self.tabBarController.tabBar.hidden=YES;
This approach will lead to push the process tabbar disappear. It does not solve the problem.
Try this code:
self.navigationController.view.backgroundColor = [UIColor whiteColor];
And I thought it is similar to this post:Dark shadow on navigation bar during segue transition after upgrading to Xcode 5.1 and iOS 7.1

How to transition smoothly from translucent to opaque UINavigationBar iOS?

I'm running into problems reconfiguring the UINavigationBar on iOS 7 and 8 when transitioning between views.
My application currently contains the following UIViewController flow:
VC1 --> VC2 --> VC3
In this flow
VC1 is the home screen and has an opaque UINavigationBar
VC2 has a translucent UINavigationBar
VC3 goes back to having an opaque UINavigationBar
The problem I've been running into is that the transitions between these views are all very sloppy looking. To start with I tried the following:
in VC2
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar configureTranslucentAppearance];
}
And in VC1 and VC3
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar restoreDefaultAppearance];
}
Here are the implementations of the two helper functions listed above:
- (void)restoreDefaultAppearance {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor JTTextNavBar]}];
[self setTintColor:[UIColor JTTextNavBar]];
[self setBarTintColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setBackgroundColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
[self setShadowImage:[UIImage navigationBarShadowImage]];
[self setTranslucent:NO];
}
- (void)configureTranslucentAppearance {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self setBackgroundColor:[UIColor clearColor]];
[self setShadowImage:[UIImage new]];
[self setTranslucent:YES];
}
This is the most basic way of handling this transition. It has the following visual artefacts:
When going from VC1 --> VC2 the moment you begin the transition the navigation bar turns black. The animation completes normally
When going from VC2 --> VC1 the nav bar instantly changes to the application default colour before the segue has time to complete.
When going from VC2 --> VC3 the navigation bar instantly goes from translucent to the app nav bar color and then menu items and VC body animate in.
When going from VC3 --> VC2 the nav bar instantly turns black and remains this way until the segue is complete.
None of these transitions look good at all. Ideally I would like the views to transition smoothly along with their new UINavigationBar but the only way I've seen to do this successfully is to manually add a toolbar to each xib.
Any suggestions? Apologies if this description is confusing :(
Edit: Added cropped images of the UINavigationBar and top portion of UIViewController for each of the listed transitions.
I finally found a decent solution!
There doesn't appear to be a proper way to smoothly transition from an opaque to transparent UINavigationBar BUT you can transition smoothly from a view controller with a visible status bar to one that has a hidden status bar.
This opens up a possible workaround which is to add the following in the viewWillAppear of VC2 from above:
[self.navigationController setNavigationBarHidden:YES animated:YES];
Once you have that, manually add a UINavigationBar to your xib and configure it to be transparent (and add all necessary UIBarButtonItem and views).
If everything is hooked up properly transitioning from VC1 to VC2 will hide the UINavigationBar at the same speed as the view transition and VC2 will show up with it's embedded UINavigationBar
Note: To make this work properly you'll have to make sure that in the viewWillAppear of View Controllers that can be accessed from VC2 you reset the UINavigationBar to be visible (if necessary) via:
[self.navigationController setNavigationBarHidden:NO animated:YES];
TL;DR - manually add a UINavigationBar to your transparent nav bar view controller and in its viewWillAppear hide the default one via setNavigationBarHidden:animated:
The black color you're seeing is the background color of the UINavigationController's view. One way to minimize seeing it is to manipulate the background color of that view to the color of the outgoing / incoming view controller's view. This works well if you're working with solid colors. Another approach is to extend your views behind the opaque navigation bar using UIViewController.extendedLayoutIncludesOpaqueBars = YES;
Set the UIWindow background color to your Navigation bar's tintColor.
always use Translucent, plus add an uiview with a color below it and animate it's alpha?
I've struggled with a nearly identical problem. There really aren't any smooth transitions using either a nav bar or toolbar as a blur. The best option that I've found* is to make an image out of the view and then use that image for the transition. Especially if you just need it for transitions, it's just about the cheapest option that still provides a great UI/UX.
*The one caveat is that some of the UI effects in a nav bar and toolbar don't show up when you take a snapshot, screenshot, or rasterize a UIView as an image. This is negligible if used for a transition.
You can create your own navigation bar using just a UIView and that way you have complete control over its appearance, layout, fading etc.
I gave up using UINavigationBar a while ago as it can be a pain to work with, as you are discovering, and now I never ever use it.
I have a root view controller which has a UIView which represents the navigation bar. If I want to do something like add a back button, change the color, show the navigation bar or hide it, change the transparency, etc. that is all controlled by the RVC and other view controllers call methods on the RVC to change the navigation bar depending upon their appearance requirements.
The RVC has a container view which is the full size of the controller view and the other view controllers get loaded into that.
A little bit of configuration to get everything set up, but once done its a structure that can be used in every project that uses a navigation bar very quickly.
So I struggled with this too.
Unfortunately I didn't have any success adding my own navigation bar via the storyboard. Instead, (and I warn you this is hacky) I add a view in the ViewController with the opaque navigation bar that has a negative margin and extends under the navigation bar.
When the ViewController with the transparent navigation bar is pushed the bar then immediately becomes transparent but due to the identically coloured view I have place directly behind it the change isn't noticeable. Et voila.
The code I have is pretty basic and written in C# (Xamarin) but for reasons of completeness....
var backing = new UIView(new CGRect(0, -68, this.View.Frame.Width, 64f));
backing.BackgroundColor = UIColor.Green;
this.View.AddSubview(backing);
Change background color work for me
window?.backgroundColor = Color.red.toUIColor()
I have one more solution..for the ones who are now refactoring the project and cant add navigation bar everywhere.. This is not a perfect solution just like #alexgophermix one. But its not bad either:
let transition = CATransition()
transition.duration = 0.6
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
transition.type = kCATransitionFade
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.layer.add(transition, forKey: nil)
navigationController?.view.backgroundColor = .clear
I am setting my nav bar in viewDidLoad() so this works everywhere and gives a slight different transition. In my case I was setting clear backgrounds somewhere and rest a gradient image. So this small change goes well for everywhere. This transition is not much noticeable but neither looks bad :)
In my case, the black I was seeing was the navbar's background image. Instead of setting it to nil, I set it to match the background color of the view behind the transparent navbar inside viewWillAppear:
let image = UIImage(color: .white)
navigationController?.navigationBar.setBackgroundImage(image, for: .default)
Then, inside willMove and viewWillDisappear I reverted it back to the original color.
YPNavigationBarTransition is a configureable framework animating navigation bars.
YPNavigationBarTransition uses two fake navigationbars (UIToolBar indeed) to simulate bar transitions while making the real navigationbar transparent.
Subclass your own UINavigationController and embed transitioncenter in it like YPNavigationController
Implement YPNavigationBarProtocol for your content controllers respectively.
Done.
A complete demo is include in the repo, checkout it for more details.

How can I set the correct Nav Bar tint color?

I am creating a title view in my navigation bar. I designed my title view with background view color R:255 G:182 B:22 (on photoshop). In my storyboard, I've set the navigation bar tint color with the same RGB code, default style and translucent checked.
I put my image on my navigation title using:
UIImage *titleImage = [UIImage imageNamed:#"Icon-Small-40.png"];
self.navigationItem.titleView = [[UIImageView alloc]initWithImage:titleImage];
When I run the app, you can clearly see a square icon in the title view. The title view has a slightly darker background. I want to make the background the same color. I get the feeling that it has something to do with some storyboard setting but I can't find the issue.
the view has a tintColor property, so
self.navigationItem.titleView.tintColor = ...
Below answer for your navigation bar tint color
self.navigationController.navigationBar.tintColor = [UIColor greenColor];
Trying to make colours match up like this can be maddening. Colour space problems aside, the actual colour of the navigation bar isn’t guaranteed by the API to be the same as its barTintColor. You might find it easier to give the title image a transparent background.

How to set clearColor for UIPopover View?

I need to set UIPopover background color to clearColor. But rest of all colors are working fine with below code:
myPopover.backgroundColor = [UIColor greenColor];
but when I set clearColor, there is a white background instead of transparent.
Can any one help me. Thanks in Advance!
If what you're talking about is a UIPopoverController, then try setting the background color to its content view controller's view.
myPopover.contentViewController.view.backgroundColor = [UIColor clearColor];
Also, check this SO post about custom popover backgrounds.

Resources