Add a button to float on top of a ViewController, Path inspired - ios

I am trying to build a button that will be floating on top of a ViewController, it should remain in the same position statically while the viewcontroller beneath it can scroll up and down.
I designed my own button class and in the viewdidload of the parent Viewcontroller I am doing this :
- (void)viewDidLoad// this is the parent viewcontroller
{
[super viewDidLoad];
customButton *floatingButton = [[UIButton alloc]initWithFrame:(self.view.frame)];
[floatingButton setLocation:CGPointMake(300, 430)];
[self.view addSubview:floatingButton];
[self.view bringSubviewToFront:floatingButton];
This is not doing it, as in when I run the button doesn't show up, only the view controller.
How can I achieve this?

I don't think that you're initializing your button properly. initWithFrame is a UIView initializer, being called by a regular UIButton. I don't see how you would get anything but an empty view here.
If you have a custom initializer for your custom button you should use
[[CustomButton alloc] myCustomInitializerWithFrame:frame];

Thanks guys, but the error was on my end, I wasn't initializing the frame properly, so the button was there but had a height and width of (0,0).

Related

Is it possible to format text in a scroll view in a storyboard which is longer than one screen? [duplicate]

I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
#synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
Hopefully this is clear. Thanks for all your help!
UPDATE
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
Here's an example of how to set it up in Xcode:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
Here's what happens when I run it in the simulator:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
So, an example to you how to add a Button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
Cheers,
Kel

IOS Adding subview from other class programatically

i've tried to create a custom view which works like a bottom bar and it worked
Right now this function is required on multiple classes, so i try writing it into a new class and import it which likes:
//BottomBarLauncher.h
#import <UIKit/UIKit.h>
#interface bottomBarLauncher : UIViewController
-(void)launchBottomBar;
#end
And implement it as :
//BottomBarLauncher.m
-(void) launchBottomBar{
for (UIView *subView in [topView subviews]) {
[subView removeFromSuperview];
}
UIView *btnBarView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 53.3, 320, 53.3)];
btnBarView.backgroundColor = [UIColor redColor];
[self.view addSubview:btnBarView];
}
Now here's the problem, while i try implement it on a new view like follows:
//NewView.m
#import "BottomBarProtocol.h"
#interface NewView()
{
BottomBarLauncher *btnBar;
}
#end
//blahblahblah
[btnBar launchBottomBar];
and nothing happens, i think the problem was with
[self.view addSubview:btnBarView];
but i have no idea how to select the current view as target which i can add subview onto.
First a suggestion, looking at your requirements/code I think you want to create custom view. For creating a custom view, create a class which inherits from UIView rather than creating a UIViewController.
Now moving to the code, your btnBar is a UIViewController which has its own view self.view so when you call this [btnBar launchBottomBar] internally you are adding the bottom bar on self.view that is your btnBar controllers view and not on NewView controllers view. Hope you understand what I am pointing out.
Here you are missing out few calls,
btnBar.view.frame = CGRectMake(0,self.view.bounds.size.height-40,self.view.bounds.size.width,40); // Add suitable frame.
//This call will add the btnBar's view as subview onto your current view controller's view.
[self.view addSubView:btnBar.view];
This is not correct/recommended way and you can face serious challenges regarding memory leaks. To avoid those mistakes, as I suggested, create a custom UIView instead. Take a look around on how to create custom views.
Hope that helps!
You can return the UIView form launchBottomBar method and add as a subView in your current ViewController class
Make custom class and delegate and add that view in window and set its frame so that it is not visible and set its frame and slide from bottom when needed so you can use it in all view controller.
Thanks.

How To Put UIPickerView Outside UIView when it's loaded

I already have the code to animate UIPickerView to act like iOS keyboard, but the problem is when it's loaded for the first time, the UIPickerView is already shows up. I want to hide it first outside UIView area, until UIButton tapped. so it will animate just like iOS keyboard.
here's the picture to illustrate what I mean :
how can it be done? thank you.
Just set the frame of pickerView accordingly while initiating.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(100, 900, 200, 200)];
if you want pickerView to appear from bottom then increase the y co-ordinate and if you want pickerView to appear from right then increase the x co-ordinate value
if pickerView is on the UIView which is again on some UIViewController then set the ClipsToBounds to YES.
[yourView setClipsToBounds:TRUE];
Hope this will help you.
Sorry i cant comment so answering from this I think you are already placed an UIpicker view in your view concept is same as #xman said. If you didnt place picker view place it
then in .h create property for picker view
#interface ViewController : UIViewController
{
IBOutlet UIPickerView *statepicker;
}
#property (weak, nonatomic) IBOutlet UIView *statepickerview;
in .m first synthesize
#implementation ViewController
#synthesize statepickerview;
in viewdidLoad initially set frame of picker view out of view example
- (void)viewDidLoad
{
statepickerview.frame = CGRectMake(-9, 506, 367, 263);
[super viewDidLoad];
}
then in action call the frame to visible postion
- (IBAction)ButtonPress:(id)sender {
statepickerview.frame = CGRectMake(0, 250,321, 260);
}
when you press button if it is not visible then try to change the co-ordinates
If this not works properly check this link create view and place the picker view inside the custom view and change the frame size of the view
see this link surely it will help
You can set pickerview's y position with your window's height. Like this :
[self.view addSubview:pickerView];
pickerView.frame = CGRectMake(0, appDelegate.window.frame.size.height,pickerView.frame.size.width,pickerView.frame.size.height)
You have to add pickerview in your view and then set frame of it.
I found answer... case closed... it's all because of 'AutoLayout' is enabled on my XCode Project... when it's disabled, everything works as I expected.

How to add objects to a UIScrollView that extend beyond UIView from Storyboard?

I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
#synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
Hopefully this is clear. Thanks for all your help!
UPDATE
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
Here's an example of how to set it up in Xcode:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
Here's what happens when I run it in the simulator:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
So, an example to you how to add a Button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
Cheers,
Kel

Add clickable and fixed subview to UITableViewController?

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:
[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];
The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.
Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?
Thank you in advice!
Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!
First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.
But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:
As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)
So, my code is now looking like this:
#synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
[self resizeTableToFitBanner];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bannerView];
// some other code
}
BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:
https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html
I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

Resources