Slide down modal navigation - ios

I have a UIViewController which contains a button. On the click of the button, it transitions as a modal (slide up transition) to a TabViewController which has two tab items.
On both the tab items, I have a Close button on which I want to transition back to the UIViewController mentioned above but in a slide-down transition.
How can I do that?

Let us say, you have button close target as dismissMe,
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 75);
button.titleLabel.text = #"Close";
[button setBackgroundColor:[UIColor greenColor]];
button.center = self.view.center;
[button addTarget:self action:#selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
then please add the following code to target method.
-(void)dismissMe {
[self dismissViewControllerAnimated:YES completion:nil];
}
Also you can use this instead
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

From the view controllers with the close button, call this when the button's UIControlEventTouchUpInside event is raised:
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Related

#Selector nothing happen

I Know this is probably very easy question, but i can't do that, i am using #selector to dismiss a view, writing this code, nothing happen:
- (void)viewDidLoad {
//here create my button
dismiss = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismiss.frame = CGRectMake(100, 10, 60, 50);
[dismiss setTitle:#"Close" forState:UIControlStateNormal];
[dismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dismiss addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dismiss];
}
//here the function
- (void)closeView {
[self dismissViewControllerAnimated:YES completion:nil];
}
But nothing happen, where am I doing mistake?
thanks
Try writing the addTarget line of code after [self.view addSubview:dismiss]; and check
That depends on what you're trying to do. dismissViewControllerAnimated:completion: will dismiss a controller that has been presented with presentViewController:animated:completion:, if there is no controller presented, it won't do anything.

How to make buttons that go to a different screen?

Hey I'm having a lot of trouble with this idea I have. How do I make buttons that go to a different screen but on the CODE?
Basically depending on a value that may or may not be different for every user (Let's say the value is in x already) it will make a list of buttons...
When you click the list of buttons, they each individually go to a different screen.
How do I do this? I know how to on storyboard.. but code wise?
Edit: I SHOULD note that for the buttons specifically I have this and it is working in the code (didn't use storyboard):
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[self.view addSubview:btn];
First add a target to your button:
//Add target to your button, to call a method that presents the next ViewController:
UIButton *yourButton = [[UIButton alloc]init];
[yourButton addTarget:self action:#selector(goToNextPageMethod) forControlEvents:UIControlEventTouchUpInside];
User one of the two, case you are using a UINavigation controller or not, place them in the method you are calling when the button is pressed (the target)
//Normal presentation
YourViewController *vc = [[YourViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
//Navgiation bar
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
Hope this helps
You can attach an action to your button by doing the following:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
and then in your someAction method, you push the view modally or via your navigation controller
- (void)someAction
{
//init your view controller here....
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

How to Custom UINavigationController?

I have design goal like picture in this:
The problem is, UIButton valign cannot in center position, UIButton always relative to UINavigationController like this:
How to make UIButton valign in center of background image height not UINavigationController height?
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:#"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
check this link also.
Update:for back button
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}
First create custom button and then add subview in navigation controller :-
UIButton * headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[headerButton addTarget:self action:#selector(headerButton1Action:) forControlEvents:UIControlEventTouchUpInside];
headerButton.frame = CGRectMake(95, 1, 30,38 );
[headerButton setImage:[UIImage imageNamed:#"header-icon"] forState:UIControlStateNormal];
[headerButton setImage:[UIImage imageNamed:#"header-icon-pressed"] forState:UIControlStateSelected];
[self.navigationController.navigationBar addSubview:headerButton];
I hope this will help you.
Happy coding...
This is a good quick solution
myButton.titleLabel.font = [UIFont fontWithName:#"FontName" size:20.0];
myButton.contentEdgeInsets = UIEdgeInsetsMake(3.0, 0.0, 0.0, 0.0);
and hide your navigation bar by
[self.navigationController setNavigationBarHidden:YES];
then show your custom view
There is trick,
You can Hide navigationController using [self.navigationController setNavigationBarHidden:YES];
and display custom view in top as you want.
the prob you are facing because of the navigation bar height. Now there are two approaches. Either you can create a category for navigation bar and override the -(CGRect)sizethatFits method like given below:
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(320,100);
return newSize;
}
Or you can hide the navigation bar like :
[self.navigationController setNavigationBarHidden:YES];
and put a header view and handle that.
Do what you would prefer to do..

How to make an animation of "cancel" button when touch the UITextField

I am now creating an App that has a browser like Safari:
There is a UITextField(left side) and UISearchBar(right side) inserted into the UINavigationBar on the top of the view.
I am wondering if there is a way to add an animation on a "Cancel" button at the time it appears when user touches the UITextField.
The animation is just like it is on the Safari: when User touches the UITextField, the "Cancel" button comes from right outside of the window while the UISearchBar is fading.
Note that I am initializing the "Cancel" button by UIBarbuttonItem(UIBarButtonSystemItemCancel).
Any help would be appreciated.
Update:
Finally I found the anwser by myself. It seems that you cannot assign a specific position where UIBarButtonItem shows up or hide. So one possible way to solve this problem is to customize a UIButton just looks like the UIBarButtonItem, here is the code:
-(void)viewDidLoad {
......
CGSize backgroundStretchPoints = {4, 2};
UIImage *image = [[UIImage imageNamed:#"UINavigationBarDefaultButton.png"] stretchableImageWithLeftCapWidth:backgroundStretchPoints.width
topCapHeight:backgroundStretchPoints.height];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(330, 23, 80, 30); // initialize the button outside the window
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:#"Cancel" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
[button addTarget:self action:#selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navigationBar addSubview: button];
.......
}
and when you click the UITextField(i.e.), the cancel button will slide into the window:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25 animations:^{
button.frame = CGRectMake(232, 23, 80, 30); // here is the position button will slide into window with animation
}];
}

UINavigationController not popping view on back button

I have a UINavigationController. I've got a UIViewController that I've pushed onto the stack using pushViewController. The previous view controller has a backBarButtonItem simply titled "Cancel."
While the new view animates in correctly, when I tap Cancel, the navigation bar animates as if the view was popped, but the new view doesn't go away. Do I need to implement a delegate somewhere?
Try this,
First Create a UIButton then Create one UIBarButtonItem with Custom view, considering UIButton as custom view for UIBarButtonItem.
Consider button to target event for popping view controller.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 35, 35);
[button setImage:[UIImage imageNamed:#"dots.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backBarButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = backBarButton;
- (void)backBarButton:(id)sender {
NSLog(#"%s", __FUNCTION__);
self.navigationController.navigationBarHidden = YES;
[self.navigationController popViewControllerAnimated:YES];
}

Resources