How to present resized UIViewController in UITableViewController - iPhone - 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];

Related

Present UIViewController as a modal with transparent background

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.

Scroll to top when tapping the status bar

I got a view controller (lets call it MainViewContoller) that's present 3 different tables (one in a time), user can tap a segment control to switch between those tables.
To present those 3 tables, MainViewContoller has 3 other view controllers (A, B and C), each has a UITableView as a subview and handle it's own data.
When a MainViewContoller is loaded, it initiate controllers A, B and C, and add their tableViews to it's view:
- (void)viewDidLoad {
[super viewDidLoad];
ViewControllerA *vcA = [ViewControllerA alloc] init];
[self.view addSubview:vcA.view];
ViewControllerB *vcB = [ViewControllerB alloc] init];
[self.view addSubview:vcB.view];
ViewControllerC *vcC = [ViewControllerC alloc] init];
[self.view addSubview:vcC.view];
}
So for example when user tap the segment control and choose A, the MainViewContoller hide tables B and C, and unhide table A. Something like this:
if (userTapOnA) {
self.viewControllerA.tableView.hidden = NO;
self.viewControllerB.tableView.hidden = YES;
self.viewControllerC.tableView.hidden = YES;
}
The problem:
When user tap the status bar I want that the current visible table will scroll to top.
This behavior is pretty basic and one gets it for free when using a regular view controller, but as you can see my view controller is not regular.
I suppose that by using other controllers view as MainViewContoller view I break the default behavior, so my MainViewContoller doesn't handle the status bar tap.
Someone got an idea how to solve that?
This is directly from the UIScrollView header file:
/* When the user taps the status bar, the scroll view beneath the
touch which is closest to the status bar will be scrolled to top, but
only if its scrollsToTop property is YES, its delegate does not
return NO from shouldScrollViewScrollToTop, and it is not already at
the top. On iPhone, we execute this gesture only if there's one
on-screen scroll view with scrollsToTop == YES. If more than one is
found, none will be scrolled. */
#property(nonatomic) BOOL scrollsToTop; // default is YES.
So in your case, set all scrollsToTop to NO, except the one you want to enable at that particular moment.
You should register your nested controllers as child controllers.
[self addChildViewController:vcA];
[self addChildViewController:vcB];
[self addChildViewController:vcC];
I'm not sure if this will help to solve your issue, but that's the right way to do it.

Whats the difference between a destination vc and a source vc?

im trying to learn how to perform custom segues so i can have a game menu, and when i searched on the web it seems that you have to make a custom segue class, and override -(void) perform and in that method you have to specify a made up destination vc and a source vc. and establish its location and stuff. This was the one of the code things i saw on the internet.
#implementation FromTopReplaceSegue
-(void)perform{
UIViewController *dst = [self destinationViewController];
UIViewController *src = [self sourceViewController];
[dst viewWillAppear:NO];
[dst viewDidAppear:NO];
[src.view addSubview:dst.view];
CGRect original = dst.view.frame;
dst.view.frame = CGRectMake(dst.view.frame.origin.x, 0-dst.view.frame.size.height, dst.view.frame.size.width, dst.view.frame.size.height);
[UIView beginAnimations:nil context:nil];
dst.view.frame = CGRectMake(original.origin.x, original.origin.y, original.size.height, original.size.width);
[UIView commitAnimations];
[self performSelector:#selector(animationDone:) withObject:dst afterDelay:0.2f];
}
- (void)animationDone:(id)vc{
UIViewController *dst = (UIViewController*)vc;
UINavigationController *nav = [[self sourceViewController] navigationController];
[nav popViewControllerAnimated:NO];
[nav pushViewController:dst animated:NO];
}
#end
i guess this is supposed to make a segue that appears from the top going down or something. but i have a few questions like, what is a source and dest vc, and also in the part where the code says dst.view.fram=CGRectMake(dst.view.frame.origin.x, 0-dst.view.frame.size.height,
what the heck is that 0 there, and shouldnt that be dst.view.frame.origin.y ?
well, anyways i wired this up and created a push segue from a button, but when i did this the segue only came to about 3/4 down from the top of the screen, and the bottom 1/4 was showing the bottom of my root view. and also when i tried pressing a button on my new vc after the segue the program crashes.
Any Info would help Please!
This is one of those things that aren't fully explained it looks like. Judging from your code, it looks like the "source view controller" is the root view controller(or parent view controller) and the "destination view controller" is the DetailViewController(or the child view controller).
Not sure if this confuses you more. Anyways, the parent/root view controller is the main view controller and it handles displaying of child view controllers. What I find interesting is that the creator subclassed the view of another class rather than presenting it.
Now 0 - self.view.frame.size.height = -320 on the y axis. Think of your main view as a piece of paper and think of the child view as a second piece of paper. Imagine placing the two pieces of paper one above the other(not on top.) The screen will only display the bottom piece of paper until you animate the "paper above it" down. Basically, he plans to animate from the top rather than the bottom(I guess I answered my own curiosity.)

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 :)

All the app in iphone appear in a "grid icon" layout, how can I achieve that layout?

when u first open up iphone, all the app are layout in a "grid icon" type. And if u have too many app, the user swipe to the right, and the new view come out, with again all the app appear in a "grid icon" layout. Can u guys point me to where I can achieve such a design. Code would be very appreciated !!!
I did try something and here is what I got so far.
In my delegate.h class I have
UITabBarController *tabBarController;
View1 *view1; //Inherit from UIViewController
View2 *view2; //Inherit from UIViewController
In my delegate.m class I have
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init]; //Create a tab bar
view1 = [[View1 alloc] init]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
view2 = [[View2 alloc] init]; //create the second view
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:view2];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
So now I have two tab that load view1 and view2, they both implement UINavigationController, meaning that if I create another view3, when I pushViewController, I can create the animated effect like the iPhone. Then in view3.m when I try to go back, I can popViewController. However what I cant achieve is, let see that each view I will have 4 icon, so when I query back from the db, I know I have to display 12 icon, meaning 3 views. But I only know what information at runtime :( . As it is right, I do actually have view1, view2 and view3 as view1.m, view2.m and view3.m. If the number of icon go above 12, meaning I need another view then I am screw. Help please
I would take a look at Three20, an open-source framework that serves as the backbone for the Facebook app, and many others. In it, they have a class called the TTLauncherView the is EXACTLY like the current (as of this writing) Facebook launcher. It is very close to the functionality you get with the iPhone home screen, complete with page swipes, reordering, wobbling and deletion.
#Julien's not exactly right. What you're talking about are views that are added to a UIScrollView.
Here's an example of a scroll view
Search here for UIScrollView or check out Apple's examples (see link above).
Once you have the UIScrollView implemented, you add UIButtons or UIViews to the scrollView laid out in Grid format. Here's an example gridView project. There are others do a search here or Google to find them (e.g. iPhone GridView or iPhone Open Source GridView)
Lots of the solution here seems to be overkilled. Here is how I do it. Create a UIViewControllerTemplate, that contain 4 (or more) customize buttons on it. So every time you create a new view, you will have a layout that will look like 'grid'
-(void) initButtons{
//button size 100 X 100
int shiftx = -5;
int shifty = 15;
(self.button1).frame = CGRectMake(40-shiftx, 50-shifty, 100, 100);
(self.button2).frame = CGRectMake(180-shiftx, 50-shifty, 100, 100);
(self.button3).frame = CGRectMake(40-shiftx, 250-shifty, 100, 100);
(self.button4).frame = CGRectMake(180-shiftx, 250-shifty, 100, 100);
}
-(void) viewDidLoad{
[self initButtons];
[self.view addSubView:self.button1];
[self.view addSubView:self.button2];
[self.view addSubView:self.button3];
[self.view addSubView:self.button4];
[super viewDidLoad];
}
Just to dust this topic off a bit, Apple now has a UICollectionViewController to achieve the grid effect. I would implement it by placing it into horizontal UISCrollView attached to a UIPageViewController.