Present UIViewController as a modal with transparent background - swift

I'm trying to present a viewcontroller with a transparent background on both iOS 7 and iOS 8.
Just by changing the viewcontroller's modalPresentationStyle property to FormSheet I can get it working on iOS 7.1.
What I want is a universal way to that on ios7+
I have tried using other options to modalPresentationStyle, like: OverCurrentContext, CurrentContext and PageSheet.
I also tried to use the modalPresentationStyle.Custom but didnt have any success.
I have NavigationController if that helps in anything.
The code for the presenting view controller:
InfoViewController *info = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[self presentViewController:info animated:YES completion:nil];
And the code for the viewDidLoad(which I think has a relevant part on this) of the presented ViewController:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.modalPresentationStyle = UIModalPresentationStyle.PageSheet
}
I´m using swift and Xcode 6.
Here´s a screenshot of what I have now and of what I want, respectively:
Here's an example code: https://github.com/pbassut/TransBackgroundViewController

For those still with this problem before presenting the UIViewController set the modalPresentationStyle of the presented UIViewController to .Custom and it will work on iOS 8(Xcode 6.1). That is, you should set it in the presenting UIViewController

I've tried this solution and it works on both iOS 7 and 8:
if (UIDevice.currentDevice().systemVersion.integerValue >= 8)
{
//For iOS 8
presentingViewController.providesPresentationContextTransitionStyle = true;
presentingViewController.definesPresentationContext = true;
presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
//For iOS 7
presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
Note: Be aware of the difference between 'presentingViewController' and 'presentedViewController'.

None of the above worked for me. To get this working on iOS 10, I simply:
presented.modalPresentationStyle = .overFullScreen
presenting.present(presented, animated: true)
Then set the presented view controller's root view to have a transparent or semi transparent color. Don't change its alpha or all of its subviews will have the same alpha.

I was able to achieve this with no code:
In the storyboard, on the presenting view controller (i.e. before you segue to the one you want to be transparent), go to the attributes inspector. On that tab, there are checkboxes for "Defines Context" and "Provides Context". Check those.
On the segue, set it to be "Present Modally".
On the destination/presented view controller, go to the attributes tab. In the "Presentation" drop down, select "Over Current Context".
On the destination view controller, set it's view to have a clear background.
On the destination view controller, slap down 2 UIViews: One is your "transparent" one, and the other is your "content" one. Set the transparent one to have whatever alpha you like and put all your junk in the "content" one.

I achieve transparent with various styles in Swift 2.0 by following below steps. Analyse it to compatible with your code.
The transparent view controller(modal view controller) is launched by segue when a button click on main view controller.
Segue Settings:
In Main view controller:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let vc = segue.destinationViewController as! ModalViewController
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext //All objects and view are transparent
}
Modal View Controller on StoryBoard:
If you need view and all objects are transparent
If you need view only transparent and over the objects are not transparent
On your Modal View Controller
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8) //view only black coloured transparent
}
Sample Screenshots:
View and over the objects are transparent
View only transparent
For any other queries please comment here :)
If you confuse or more about with transparent view controller then watch my video tutorial for you on Youtube :D

iOS8+
Below code snippet will solve this problem in iOS8+
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:secondViewController animated:YES completion:nil];

I had a similar problem, i wanted to do something similar to the second screenshot that you present here (the brazilian/portuguese newspaper!)
I solved it like this. I inserted a UIView that covered the entire View Controller, then i went to the Attributes Inspector and i set the UIView's background color to black, with 50% opacity. Then, i inserted another UIView above the first one, and i worked on that one

If you change the view alpha, it will be applied to its subview, as it understands that the whole view is transparent.
The easy solution is change its colour with less opacity. Just select your background colour to custom
then select the RGB slider, choose your colour, and reduce the opacity.

Related

How to present resized UIViewController in UITableViewController - iPhone

I have a UITableViewController with multiple rows, when a row is pressed, I would like to present a ViewController, which has small view with Facebook and twitter buttons, on top of the table view ( the tableview still can be seen).
Although, I have resized the view of the ViewController in storyboard and have unchecked "resize view from nip" but when the ViewController is presented, its view appears as full screen.
I tried to add it as subview but then it is just added to the first row of the tableView.
can you please tell me what I'm doing wrong?
Thank you.
You mighty try something like the following:
1) Initialise your facebook view controller
2) Add your facebook view controller as a child view controller:
[self addChildViewController:self.facebookController];
3) Set the required frame:
[self.facebookController.view setFrame:CGRectMake(x, y, w, h)];
4) Add the facebook controllers view as necessary
[self.requiredView addSubview:self.facebookController.view];
I haven't tried this so I don't know if it works, especially since I haven't done anything with Storyboards yet. It's still worth a try.
If you only want to share info you can use UIActivityViewController,
//Include an array of things being attached to the ActivityViewController
//The Array cannot be nil, you must provide something. Either an image or text or both
NSArray *activityItems = #[#"Hello Share",[UIImage imageNamed:#"someImage"]];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact ]; // means this item you dont want to show while sharing
[self presentViewController:activityViewController animated:YES completion:NULL];

NavigationBar hides TableView in TabBarViewController

I have a ViewController that Pushes a TabBarViewController. Inside that TabBar View Controller I have 4 tabs. Two of these tab bars are UITableViewControllers and the other two are ViewControllers. The first tab is a table view controller and is working fine, ie not being hidden by the navigation bar. The third tab, which is another TableViewController, is being partially covered by the navigation bar. The first section and first cell is being hidden underneath the navigation bar. Has anyone had this problem in the past or does anyone know a solution to this? I've tried a couple of things like resizing the frame size manually
self.tableView.frame = CGRectMake(10,10,self.view.bounds.size.width -20, self.view.bounds.size.height-20);
That did't seem to work. I tried AutoLayout as well and didn't work. I don't know what else to do. Anyone have any suggestions or ideas of how to tackle this.
Edit: I've tried the edgesForExtendedLayout but it is making my navigationbar a darker color. It animates the color change in the navigationbar, sort of like a loading bar.
Note: This is only happening in ios7. I just simulated it in iOS 6.1 and the navigationbar does not cover the table view controller at all, which is weird to me. Any one have any suggestions?
Edit #2: Noticing that this is an iOS 7 > problem i did the following but now the navigation bar has changed color to a darker color.
if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:#"."][0] intValue] >= 7)
{
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
In the viewDidLoad method of UITableViewController (assuming it's loaded with a call to the tabBarController) that is associated with the tabs use
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.tableView.contentInset = UIEdgeInsetsMake(0., 0., CGRectGetHeight(self.tabBarController.tabBar.frame), 0);
}
I fixed that with:
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
viewcontroller.edgesForExtendedLayout = UIRectEdgeNone;
viewcontroller.extendedLayoutIncludesOpaqueBars = NO;
viewcontroller.automaticallyAdjustsScrollViewInsets = NO;
}

Tabbar Controller with swipe effect

I'm trying to do a Tabbar Controller like below effect:
By swiping an viewcontroller will redirect to next tab. How can we achieve this in iOS? Is there any other controls to do like this?
Just add UISwipeGestureRecognizer to your tabBarView controller and change your tabBar index after swipe.
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(swipeMethod:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeRecognizer];
And my method to handle swipe is :
-(void)swipeMethod: (UISwipeGestureRecognizer *) sender
{
NSLog(#"Swipe!");
}
EDIT
Or you can use UIScrollView with paging enable and UIView to display your data.
Here is the tutorial you are looking for Tabbar Controller with swipte effect
There is a library for this on GitHub, it is called MGSwipeTabBarController and is designed to do exactly what you are looking for.
It is as simple as :
NSArray *viewControllers = . . . //your view controllers
MGSwipeTabBarController *swipeController = [[MGSwipeTabBarController alloc] initWithViewControllers:viewControllers];
Please Note that it is only compatible with iOS7 and + and that you'll still need to design your own tab bar that respond to scroll events using the MGSwipeTabBarControllerDelegateprotocol.
https://github.com/mglagola/MGSwipeTabBarController
https://github.com/nicklockwood/SwipeView
you can use this class to achieve your goal...
or else you have to make animation for tap on tabbar using following method,
[UIView transitionFromView:<#(UIView *)#> toView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> completion:<#^(BOOL finished)completion#>]
If anyone is still looking you can find another implementation here on this youtube series
https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj
Edit:
So to my knowledge and according to the video, the idea is that you'll want to use two UICollectionViews in one view controller. One collection view to display the content (apps) and the other for the horizontal navigation containing the categories.
To create the green 'highlight' bar you can use a UIView and adjust the height/width of the bar using constraints - heightAnchor/widthAnchor and add that to the navigation bar.
To get the bar moving with the distance the user will swipe, there is a method you can override to capture the horizontal scrolling called scrollViewDidScroll. From here you'll want to provide a variable from your the UIView's constraint (of type NSLayoutConstraint) to be able to update the x position of the UIView
override func scrollViewDidScroll(_ scrollView: UIScrollView)
{
print(scrollView.contentOffset.x)
menuBar.myGreenBarLeftConstraint.constant = scrollView.contentOffset.x / 4
//or however many categories you have
}

how to create pinterest style hiding/unhiding nav/tab bar?

How do I create a hiding/unhiding nav bar like what pinterest and many other apps is doing? I know the basic idea is to use the UIScrollView delegate and detect whether I am scrolling up or down and show the nav bar based on that. So should I also adjust the navcontroller view height if the nav bar is hidden? How does this work?
I have a sample project located on github that does exactly the pinterest/piictu style 'hide the UINavigationController / UITabBarController stuff'
https://github.com/tonymillion/ExpandingView
I've tried https://github.com/tonymillion/ExpandingView and ran into a bunch of issues.
I ended up rolling my own navigation controller to get all the animations synced and used this scrollview code to figure out if I should expand or contract. iOS >=5.0
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
MyCustomNavController* navController = (MyCustomNavController*)self.parentViewController;
if( [scrollView.panGestureRecognizer translationInView:self.view].y < 0.0f ) {
[navController setExpanded:YES animated:YES];
} else if ([scrollView.panGestureRecognizer translationInView:self.view].y > 0.0f ) {
[navController setExpanded:NO animated:YES];
}
}
I would probably try to create my own root controller with scrollbar as main view and put navigation controller's view into it. You can't use scrollbar inside navbar view then but I believe you don't need it in this very case.
If this approach doesn't work I would probably create my own controller that mimic navigation controller appearance.

Tab Bar controller is not accessible

I have created a tab based application for iphone. when the 1st tab presses a first view will present. this view contains a button, on pressing it another view loads.
Code is:
-(IBAction)buttonPressed: (id) sender
{
Cities *cv=[[Cities alloc] initWithNibName:#"Cities" bundle:nil];
cv.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:cv animated:YES];
[cv release];
}
Now problem is that this view is loading in whole screen so that I am not able to access tab bar.
I have set the frame for this view and the view is loading in this frame,
-(void)viewWillAppear:(BOOL)animated
{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 400);
}
but in remaining part white screen is appearing means tab bar is not accessible.
I want that whatever will be load at any time tab bar should be always accessible.
Please help me out.
Add
cv.modalPresentationStyle = UIModalPresentationCurrentContext;
Have you tried using UINavigationController inside your tabbar to dig inside your UIViewControllers??
for Ref : Adding NavigationController to Tabbar
do you really need a viewController Class for what you are trying to display??
if der's no core functionality being used, i think it will be much easier with UIView.
Happy Coding :)