I've subclassed UINavigationBar in order to set a custom background image.
I'm using it to initialize a UINavigationController with:
-(id)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
The problem is that when new UIViewControllers get pushed on to the NavigationController stack, the title text animates in from the right across the background image in a way that looks a bit sucky.
Problem: How to tweak the animation so e.g. it fades in as it animates across.
The closest I've come is by creating a custom fade-up animation in the pushed/popped ViewController class. I use a NSTimer to trigger changes to the alpha of the titleView:
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
...
-(void)setAlpha:(float)alpha
{
titleView.textColor = [UIColor colorWithRed:grayShade green:grayShade blue:grayShade alpha:alpha];
}
However, if I trigger this in 'ViewDidAppear' it's already too late and the fade-up starts after the new view has finished sliding in. I figured that I could trigger it in 'ViewWillAppear' but that this might lead to indeterminate and inconsistent timings on different devices. Am I right in this assumption and if so, how can I customize the animation in the way I want to?
Related
I'm loading a view controller modally via another view controller and I'm trying to change the background color using:
override func viewDidLoad() {
super.viewDidLoad()
transparentBG.backgroundColor? = UIColor.black.withAlphaComponent(0.4)
// transparentBG is a UIView defined in storyboard
}
While the view is animating into position (sliding up) it maintains the alpha value I set. But once it reaches the top of the screen it removes the alpha component and is changing the color to what looks like the color with the alpha component, so like a gray color, but with no transparency as seen in the image below.
Is there anyway to maintain the alpha component after if finishes loading?
Step one: Change this to an overFullScreen presentation.
Step two: There is no step two.
just set presentaion style on viewContriller
[myViewcontroller setModalPresentationStyle:UIModalPresentationCustom];
[myViewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self.navigationController presentViewController:myViewcontroller animated:true completion:nil];
What is happening is that the alpha is being kept, but the previous view is being removed once the animation is complete.
There are a couple approaches you can take.
Take a screen shot of the previous view and insert that as a background in the new view. Look at the drawViewHierarchyInRect function. You can grab the screen shot in the new view controller's init method, then set it as a background image in the viewDidLoad.
The other approach would be to add the overlay as a subview, either to the existing view, or even the window itself.
I've used both methods successfully.
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.
I have a bunch of animations set to repeat that work beautifully if they're in their own view controller that is pushed to, but if I modally present them (via a modal segue from a UIButton tap) suddenly none of them play.
Does anyone have any idea why this may be?
This is an example of one animation being added:
UIView *topTapRipple1 = [[UIView alloc] initWithFrame:(CGRectMake(73, 30, 13.0, 13.0))];
topTapRipple1.backgroundColor = [UIColor clearColor];
topTapRipple1.layer.cornerRadius = topTapRipple1.bounds.size.height/2;
topTapRipple1.layer.borderColor = [UIColor colorWithRed:0.886 green:0.886 blue:0.886 alpha:1].CGColor;
topTapRipple1.layer.borderWidth = 1.0;
[self.middleContentView insertSubview:topTapRipple1 belowSubview:self.middle];
Where that's the view that is added in order to create the animation. But that view never even gets added. Why is this?
Here's an example project replicating the issue: https://dzwonsemrish7.cloudfront.net/items/163k0D2f2L2P3H0E3y2i/animationtest.zip
Since you are doing the animations in viewDidLoad, the segue animation isn't complete when you try to start them. Try putting the animations in viewDidAppear:
Edit: This kind of animation would ideally be done with CAAnimation and CALayer. Creating and adding all those subviews seems like overkill.
I have an app where up until now I've been using a UINavigationController with a UINavigationBar that has its property translucent = YES. This means the UINavigationController's content view (i.e. the views from the view controllers you push) to be full-screen (minus status bar).
However, if you set the navigationBar.translucent = NO, this container view becomes 44pt shorter, as I suppose Apple has assumed you don't need any content under an opaque navigationBar.
... except if you're doing what we're doing and are employing a navigationBar that scrolls away (see This Post on how to do that) So I'd like to know if this is possible.
I want to have translucent = NO, but have everything behave as if it were still set to YES. I like the functionality of the translucent = YES, but I don't actually want the bar to be made translucent by UIKit.
What worked for me was to add
extendedLayoutIncludesOpaqueBars = true
in
viewDidLoad
something like this
override func viewDidLoad() {
super.viewDidLoad()
extendedLayoutIncludesOpaqueBars = true
}
Hope it will work for you as well
It's not necessarily a good answer but you could just offset your view that high if you're not translucent.
//This won't take into account orientation and probably other details
if(!self.navigationController.navigationBar.isTranslucent)
{
self.view.frame = CGRectMake(0,0,-44,self.view.bounds.size.height);
}
You could put that in your viewDidLoad or viewWillAppear and if you have a bunch of view controllers you can just subclass them all and put your logic in the subclass.
I found a solution that works, although it is indeed a bit of a hack.
The idea is to give the translucent nav bar an opaque backing. Unfortunately I'm not happy with the solution in that it's dirty and not encapsulated and introduces some potential issues, but i AM happy because it got the job done.
In my Application's base view controller class (i.e. MyViewController : UIViewController), in the viewDidLoad method, I instantiate a new ivar UIView *_navigationBarBG and give it the same frame as self.navigationController.navigationBar. I then set it's backgroundColor property to [UIColor whiteColor] although this is how you achieve some more tint I guess. [EDIT:If you wanted to be a purist (color values remaining exactly as they come from the .psd), you could make the _navigationBarBG a UIImageView and use your custom background there, and the background of the actual UINavigationBar you set to draw clear (or stretch a 1px transparent image if you wanted to use a typical 'change your navigation bar using an image' recipe that's somewhere on the internet)]
if(self.navigationController)
{
_navigationBarBG = [[UIView alloc] initWithFrame: self.navigationController.navigationBar.frame];
_navigationBarBG.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_navigationBarBG];
}
THEN, (and this is the crappy part, but I don't see any other way), I add this view as a subview. BUT, whenever you would normally make a call to [self.view addSubview: anyView], you have to make sure you call [self.view insertSubview: anyView belowSubview: _navigationBarBG];
if (_navigationBarBG)
[self.view insertSubview: anyView belowSubview:_navigationBarBG];
else
[self.view addSubview: anyView];
If you forget that, these added views will slide under your navbar background and look weird. So you need to know that this is a source of error.
WHY AM I DOING THIS? Again you might ask... I want to be able to have a scrolling navigation bar that scrolls out of the way when you scroll down your table view, thereby giving the user more screen space. This is done by using the scrollView delegate (scrollViewDidScroll:) and also viewWillAppear:
// FIRST DEAL WITH SCROLLING NAVIGATION BAR
CALayer *layer = self.navigationController.navigationBar.layer;
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGPoint newPosition;
if (contentOffsetY > _scrollViewContentOffsetYThreshold && self.scrollingNavigationBarEnabled) {
newPosition = CGPointMake(layer.position.x,
22 - MIN((contentOffsetY - _scrollViewContentOffsetYThreshold), 48.0)); // my nav bar BG image is 48.0 tall
layer.position = newPosition;
[_navigationBarBG setCenter: newPosition]; // if it's nil, nothing happens
}
else
{
newPosition = kNavBarDefaultPosition; // i.e. CGPointMake(160, 22) -- portrait only
layer.position = newPosition;
[_navigationBarBG setCenter: newPosition]; // if it's nil, nothing happens
}
I was looking for an answer to this as I wanted my subviews to be at (0,0) and not (0,44)(in reference to the Screen bounds), but I could not find an answer on how to set this in the NavigationController, which I thought would be an included property.
What I ended up doing that was very simple is adding a subview to the navigation controller that was the width and height of the Navigation Bar, but then insert the subview below the Navigation Bar.
Now the setting is Translucent = YES, but it still appears solid and the subviews behave how I want.
EDIT: After re-reading your original post, I suppose if you're going to be rolling the nav bar away, you'll have to take into account hiding and showing the new subview as you do the same with the nav bar
Every screen of my app has a common tint. Its not a background. Its a pattern that fills the entire screen and it is top of all the views. You can see the pattern flow continuously from one view to another inside the same screen. And it neither obscures other elements nor participate in event handling.
I tried implementing it with this code in my ViewController.
UIColor* texture = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Texture.png"]];
UIView* tintView = [[UIView alloc] initWithFrame:self.view.bounds];
[tintView setBackgroundColor:texture];
[tintView setAlpha:0.5];
[self.view addSubview:tintView];
But it doesn't pass on touches to the views behind it.
tintView shouldn't participate in any event handling. Rather it should let other elements behind it, handle the events like they do it normally.
Other way of doing it is set this as a background of the view property of a UIViewController and set a common alpha for all other subviews of view to show the pattern behind. That will be redundant in most ways.
Any better way of doing this?
Make your tintView a subclass of UIView and implement the hitTest:withEvent: method, returning nil. This will make your view transparent to touches. Or set userInteractionEnabled to NO.
Set the background color with a Textured image
UIImage *bgimg = [UIImage imageNamed:#"Texture.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:bgimg];