Passing UINavigationBar to multiple UIViewControllers - iphone

So, at the moment, I am attempting to pass the same UINavigationBar to multiple UIViewControllers.
For example, say I have a UINavigationController whose view is a UIViewController's UIView. Withinn the custom UIViewController's UIView I modify the contents of the UINavigationBar's titleview.
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x + 50, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width - 100, 44)];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(35, titleView.frame.origin.y + 2, 40, 40);
[button1 setImage:[UIImage imageNamed:#"FacebookFriendRequestIcon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(FriendRequestsPopover:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(80, titleView.frame.origin.y + 2, 40, 40);
[button2 setImage:[UIImage imageNamed:#"FacebookMessagesIcon.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(MessagesPopover:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(125, titleView.frame.origin.y + 2, 40, 40);
[button3 setImage:[UIImage imageNamed:#"FacebookNotificationsIcon.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(NotificationsPopover:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:button3];
self.navigationItem.titleView = titleView;
When a user presses a button within the UIViewcontroller (not referring to the navigationBar's items), the user is then pushed to another UIViewController:
GeneralInfoViewController *generalInformationController = [[GeneralInfoViewController alloc] init];
[self.navigationController pushViewController:generalInformationController animated:YES];
UIViewController1(UINavigationController) pushes to UIViewController2(UINavigationController)
So, the generalInformationController is subclassed as a UIViewController. Now when the push occurs, I would like an animation to occur, but only with the UIView's not with the NavigationBar.
An example application would be like Facebook, the Navigationbar's middle buttons (messages, friend requests, and notifications) are ALWAYS visibile and never animate away when a viewcontroller is pushed onto the stack.
Anyone have any ideas on how this is done? I guess a theoretical answer would be nice but a coding example would be nicer :)

An example application would be like Facebook, the Navigationbar's
middle buttons (messages, friend requests, and notifications) are
ALWAYS visibile and never animate away when a viewcontroller is pushed
onto the stack.
That might be a clue that the Facebook app doesn't use a navigation controller. I don't know if it does or doesn't, but it would probably be easier to write your own container view controller that does what you want than to try to force UINavigationController to behave in a way that it's not designed to.

If you use navigation controller for pushing and poping, you can't make, that your nag bar stay on his place;
BUT you can try it:
newView.bounds = mainView.bounds; //newView can be nextViewController.view
newView.biunds.size.height=newView.biunds.size.height-self.navBar.bounds.size.height; //devide navBar height
newView.frame = CGRectMake(newView.frame.origin.x + 784, newView.frame.origin.y, newView.frame.size.width, newView.frame.size.height);
[mainView addSubview:newView];
/* Begin transition */
[UIView beginAnimations:#"Slide Left" context:oldView];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDidStopSelector:#selector(slideInCompleted:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
newView.frame = CGRectMake(0,0, newView.frame.size.width, newView.frame.size.height);
oldView.frame = CGRectMake(oldView.frame.origin.x - 784, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);
[UIView commitAnimations];
Post comments, for your result.

Related

How to add a button in UIScrollView with addSubview?

I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}

AutoresizingMask Not Working in Click Events

I have created a custom button in viewDidLoad() Method and set Autoresizingmask. It works well. But When I Put the same code in Button Click event It Doesn't Autoresized. Is there anyother way to access the AutoresizingMask within Click Events? Thanks in Advance.
Enter code here
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.backgroundColor=[UIColor redColor];
button.frame=CGRectMake(20, 373, 73, 43);
button.autoresizingMask=(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth);
[self.view addSubview:button];
}
-(IBAction)btn_clicked
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
enter code here
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}
(void)layoutSubviews
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.
With reference to Apple document in case of AutoresizingMask not working we need override the above method. AutoresizingMask works when the view frame is changed within - (void)layoutSubviews.
enter code here
-(void)layoutSubviews
{
NSLog(#"layoutSubviews called");
CGRect viewsize=[[UIScreen mainScreen]bounds];
view.frame=CGRectMake(0, 0, 320, viewsize.size.height);
}
Trying the Above code Your AutoresizingMask works well in both iPhone4 and iPhone5.
(void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame=CGRectMake(20, 73, 173, 43);
[button addTarget:self action:#selector(btn_clicked:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
-(IBAction)btn_clicked:(id)sender
{
UIButton btn = (UIButton)sender;
[btn setTitle:#"Dynamic" forState:UIControlStateNormal];
[UIView animateWithDuration: 0.4
animations: ^{
btn.frame=CGRectMake(20, 73, 100, 43);
[UIView commitAnimations];
}completion: ^(BOOL finished){
}];
}
i think you need to typecast your UIButton through its click event. make your touchUpInside event like this
-(IBAction)btn_clicked:(id)sender
{
UIButton *btn = (UIButton*)sender;
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
//enter code here
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}
And call this click like this #selector(btn_clicked:) or connect it via Interface Builder
See if this work....
Please use below code may help you:
-(IBAction)btn_clicked
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizesSubviews = YES;
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}

Adding Custom UIButton To subView

I have subview which it initiated by touch on the UIbutton in the main view.
Once the subview pops up it displays a label with the info I provided. This all works. I just need an UIbutton to show up so I can then set it up to dismiss the subview and go back to the main view. i have searched all over and have found nothing that helps.
Here is what my code in my .m file looks like:
////Button that Displays Subview with Its Label and Button
- (IBAction)showInfo:(id)sender
/////UI Subview
{UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:mySubview];
mySubview.backgroundColor = [UIColor blackColor];
mySubview.alpha = .7f;
//////Label in SubView
{UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
label.text = #"Here is info";
label.textColor = [UIColor whiteColor];
label.backgroundColor= [UIColor clearColor];
[self.view addSubview:label];
////Button in Subview
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-info-x.png"] forState:UIControlStateNormal];
//set the position of the button
button.frame = CGRectMake(120, 252, 68, 68);
//add the button to the view
[self.view addSubview:button];
Now I just need to add an action to the UIButton to dismiss the Subview?
First Take IBOutlet of subView and add it to the main View.
IBOutlet UIView *subView;
Then, add UIButton to the SubView like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//set the position of the button
button.frame = CGRectMake(0, 0, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[btnMenu addTarget:self action:#selector(your MethodName:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[subView addSubview:button];
Then,
-(IBAction)your MethodName:(id)sender {
}
In Above Method Remove SubView from Main View.
If you're simply looking to set an image for a UIButton instead of a title, you're looking for the - (void)setImage:(UIImage *)image forState:(UIControlState)statemethod.
You should check the UIButton reference for more info.
Add Target to the custom button
{
.....
[button addTarget:self action:#selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside];
.......
}
-(IBAction)your dismiss:(id)sender
{
// write the dismissal code here
}

Button click not working when in presence of single Tap gesture

I have view hierarchy as follows
MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View(added via code)
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map.png"]];
[scroll addSubview:imageView];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(660, 120, 40, 40);
[myButton setTitle:#"10" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton setEnabled:YES];
[imageView addSubview:myButton];
later on in view controller, i defined selector as
-(void) asiaPressed:(id)sender{
NSLog(#"asiaPressed clicked");
}
But it is never going inside selector..
Pls help...i guess it is very basic error..
Thanks in advance
UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:
...
imageView.userInteractionEnabled = YES;
...
P.S. You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above

Display appears ghostly, but works on first touch

I have made a UIView which contains a table and some buttons. This view is called by a UIViewController which is called by a tab-bar controller. There is no IB involved.
When the view comes up it only has the outlines of the objects. e.g. the following button comes out without its label:
brect = CGRectMake(80, 330, 60, 27);
centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:brect];
[button2 setCenter:centern];
[button2 setTitle:#"Delete" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(deldate:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
As soon as I make the first touch everything appears as it should.
What am I doing wrong?
[self.view addSubview:button2];
self is an instance of UIViewController.
self contain a view. you should add you UIButton to view, not viewController.