iOS [Obj-C] - NavigationBar transparent with visible items while scrolling - ios

This is my question
CONTEXT
I have a ViewController which I have an effect where the Navigation Bar gets transparent when the user go down in the scroll, and the Navigation Bar gets normal when the user go up in scroll view. This effect I did with the UIScrollViewDelegate's methods. This is the code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.y;
if (scrollView.contentOffset.y < 0){
scrollView.bounces = false;
}else{
scrollView.bounces = true;
}
CGFloat currentAlpha = (offset / 310);
if (currentAlpha < 0) {
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.alpha = 1;
self.navigationController.titleNavBar.alpha = 0; //This property I made in an UINavigationController extension
} else {
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.alpha = 1;
self.navigationController.titleNavBar.alpha = currentAlpha; //This property I made in an UINavigationController extension
[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:0.0f/0.0f green:136.0f/255.0 blue:206.00f/255.0f alpha:currentAlpha]];
}
}
With the previous code I got the effect, but I have a problem: I can't add it to the status bar because self.navigationController.navigationBar.translucent is set on YES. So, in the iPhones, the status bar shows transparent and in the iPhone X shows bigger this transparence than another iPhones (see the image).
Anyone know how can I do this transparent effect with Navigation Bar and the Statsus Bar?

You need to change statusBar UIView color with NavigationBar color.
Create AppDelegate SharedInastance :
+ (AppDelegate *)sharedAppDelegate {
return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
Add below code in AppDelegate to get status bar view:
- (UIView *)statusBarView {
return [[[UIApplication sharedApplication] valueForKey:#"statusBarWindow"] valueForKey:#"statusBar"];
}
Add below code when you want to change status bar color:
[[AppDelegate sharedAppDelegate] statusBarView].backgroundColor = [UIColor redColor];

Related

NavigationBar Layout has an exception when user has in call status bar

After I toggle in-call status bar, the navigation bar drops. And the content behind falls a height of 20 .
I can not figure out why.
The picture shows the scene.
Looks like the navigation bar drops. And the blue bar falls.
Seen from the view hierarchy debugger, I do not know why the blue bar has a distance from the navigation bar.
Here is the relevant code.
- (void)viewWillLayoutSubviews{
self.blueBar.translatesAutoresizingMaskIntoConstraints = NO;
[super viewWillLayoutSubviews];
[self.blueBar.topAnchor constraintEqualToAnchor: self.view.topAnchor].active = YES;
[self.blueBar.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor].active = YES;
[self.blueBar.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor].active = YES;
[self.blueBar.heightAnchor constraintEqualToConstant: 75].active = YES;
......
}
-(UIView *)blueBar{
if(!_blueBar){
_blueBar = [[UIView alloc] init];
_blueBar.backgroundColor = [UIColor blueColor];
}
return _blueBar;
}
The status bar height change from 20 to 40 when in-call.And I found you use tableView,so try this:
if (#available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
What's more,why not use mainstream frameworkMasonry?

iOS 11 UINavigation Bar is messed up

With iOS 11 navigation bar's title view and bar button item is not centered.
Also the background image's height does not change and is not shown in full.The bar height is 74.
See the white space.
I have tried this
if(#available(iOS 11,*)){
_homeNavigationBar.prefersLargeTitles = NO;
_homeNavigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
[_homeNavigationBar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"navbarBg.png"]]];
}
else{
[_homeNavigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBg.png"] forBarMetrics:UIBarMetricsDefault];
}
But still i am unable to center the title and bar button item.
Any idea how can i fix this?Please do let me know.Thanks
Subclassing the navigation bar did the trick for me.
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
if([NSStringFromClass([view class]) containsString:#"Background"]) {
view.frame = self.bounds;
}
else if ([NSStringFromClass([view class]) containsString:#"ContentView"]) {
CGRect frame = view.frame;
frame.origin.y = 25;
view.frame = frame;
}
}
}

How to handle subviews of UITabBarController when interactivePopGesture (like Flipboard)

I'd like to have an underline that indicates which item was selected. It slides to any other items whenever the item was tapped. Therefore, I added a subview to the custom UITabBarController and set the animation. Then I use hidesBottomBarWhenPushed to hide the tab bar when pushed. However, the underline seems not combined with the custom UITabBarController.
How to handle the subview so it is always on top even when using the back gesture? This Flipboard app capture is what I want to do.
Edit:
CustomTabBarController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// create underline view
CGRect tabBarFrame = self.tabBar.frame;
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarFrame) / MIN(5, self.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * self.selectedIndex;
CGRect underlineFrame = CGRectMake(originX, CGRectGetMaxY(tabBarFrame) - 3.0f, itemWidth, 3.0f);
self.underlineView = [[UIView alloc] initWithFrame:underlineFrame];
self.underlineView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.underlineView];
}
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUInteger itemIndex = [tabBar.items indexOfObject:item];
CGRect underlineFrame = self.underlineView.frame;
CGFloat originX = (CGFloat)CGRectGetWidth(self.underlineView.frame) * itemIndex;
// underline shifting animation
[UIView animateWithDuration:0.25
animations:^{
self.underlineView.frame = CGRectMake(originX, underlineFrame.origin.y, CGRectGetWidth(underlineFrame), CGRectGetHeight(underlineFrame));
}];
}
CustomTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *detailViewController = segue.destinationViewController;
detailViewController.hidesBottomBarWhenPushed = YES;
}
hidesBottomBarWhenPushed hides the tab bar but its subview (the underline view).
If I hide it by myself and show it in viewWillAppear, the underline view does not look like on top of the tab bar.
I finally found a workaround. To override the method hidesBottomBarWhenPushed, then you can add an alternative view for tab bar's subviews.
sourceViewController.m
- (BOOL)hidesBottomBarWhenPushed
{
[super hidesBottomBarWhenPushed];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden) {
CGRect tabBarBounds = tabBarController.tabBar.bounds;
CGFloat underlineHeight = CGRectGetHeight(tabBarController.underlineView.frame);
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarBounds) / MIN(5, tabBarController.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * tabBarController.selectedIndex;
UIView *alternativeView = [[UIView alloc] initWithFrame:CGRectMake(originX,
CGRectGetMaxY(tabBarBounds) - underlineHeight,
itemWidth,
underlineHeight)];
alternativeView.tag = tabBarController.underlineViewTag;
alternativeView.backgroundColor = tabBarController.underlineView.backgroundColor;
[tabBarController.tabBar addSubview:alternativeView];
}
return NO;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden) {
tabBarController.underlineView.hidden = NO;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
[alternativeView removeFromSuperview];
}
}
Don't forget the case that interactivePopGesture failure to popover view controller, the alternative view still be added to tab bar. So remove it at destination view controller if needed.
destinationViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
if (alternativeView) [alternativeView removeFromSuperview];
}

Unable to hide status bar on iOS 6,7 when presenting viewcontroller

The following is my custom VC presentation code:
-(void)presentViewController:(UIViewController*)vc
{
UIWindow *w = [[[UIApplication sharedApplication] delegate] window];
UIViewController *parentController = (TabBarViewController *)[w rootViewController];
[parentController addChildViewController:vc];
if ([vc respondsToSelector:#selector(beginAppearanceTransition:animated:)]) // iOS 6
{
[vc beginAppearanceTransition:YES animated:YES];
}
UIView *toView = vc.view;
[parentController.view addSubview:toView];
toView.frame = parentController.view.bounds;
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, 1.0f, 1.0f);
toView.transform = CGAffineTransformScale(self.view.transform, 0.01f, 0.01f);;
CGPoint oldCenter = toView.center;
toView.center = ((RootViewControllerEx*)vc).cellCenter;
[UIView animateWithDuration:4.5 animations:^{
toView.transform = tr;
toView.center = oldCenter;
} completion:^(BOOL finished) {
[vc didMoveToParentViewController:parentController];
if ([vc respondsToSelector:#selector(endAppearanceTransition)]) // iOS 6
{
[vc endAppearanceTransition];
}
}];
}
It works fine, however, in presented VC I am hiding status bar:
- (BOOL)prefersStatusBarHidden {
return YES;
}
When I present my VC using built-in presentViewController:animated:completion:, status bar in presented VC is hidden. But with my code on iOS 7 status bar is not hidden at all, on iOS 6 it is even more strange - status bar is hidden, but my view size is shorter from top by the size of status bar. So I can see a black gap from top on iOS 6. What should I do to properly hide status bar when using custom VC presentation?
you should try this in your viewDidLoad for differencing the IOS 6/7 status bar problem
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)])
{
//IOS 7 - Status Bar Hidden
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
self.statusBarHidden = YES;
}
else
{
// iOS 6 - Status Bar shown
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
self.statusBarHidden = NO;
}
and an method for hiding status Bar
- (BOOL)prefersStatusBarHidden{
return YES;}
and also add an property for status Bar
#property BOOL statusBarHidden;
then make sure that your view bounds to the screen size and fits correctly
I think this solves your problem :)
Try this
in view did load
[UIApplication sharedApplication].statusBarHidden = YES;
and set value in plist like
set this in project summary
and this in your interface builder

Search Bar background color Gray ios7

I am currently working on an app that worked fine until ios7 came along. The search bar used to be transparent and blended into the blue background of the navigation bar. Now that I am working in ios7, the nav bar is blue, however the search bar has a gray background to it. How do I make it blue or transparent?
Here is an image:
Try this:
if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
You can set "Bar Tint" to "Clear Color" in Interface Builder (.xib):
It can also be done in code:
self.searchBar.barTintColor = [UIColor clearColor];
To make it a flat color, you simply need to remove the UISearchBarBackground view.
I created a recursive method to properly clean the search bar.
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
You can simply send your search bar to the method and change the color afterward.
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
Swift 4.2
You can use this extension to change Font and Background color of the SearchBar.
extension UISearchBar {
var textField: UITextField? {
let subViews = subviews.flatMap { $0.subviews }
guard let tf = (subViews.filter { $0 is UITextField }).first as? UITextField else { return nil }
return tf
}
func setTextColor(color: UIColor) {
textField?.textColor = color
}
func setBackgroundColor(color: UIColor) {
textField?.backgroundColor = color
}
}
**Edit - This worked for me in iOS 7
// Set the color to whatever blue color that is in your screenshot
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
If you want all of your search bar's to be a certain color do this:
// Put this in your app delegate's didFinishLaunchingWithOptions method
// Whatever color you want for searchBarColor
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
UIColor *searchBarColor = [UIColor blueColor];
[[UISearchBar appearance] setBackgroundColor:searchBarColor];
}
If you just want that particular search bar background to be a color:
// Set it in your viewDidLoad method of your controller
// Replace the yourSearchBar property with whatever you're doing to instantiate the search bar
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
{
UIColor *searchBarColor = [UIColor blueColor];
self.yourSearchBar.backgroundColor = searchBarColor;
}

Resources