How To Put UIPickerView Outside UIView when it's loaded - ios

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.

Related

access UIView In UIViewController Storyboard

I want to ask, who the Views in the Storyboard, which are attached to a UIViewController are accessable. Who to add them to the UIViewContoller to its views programmaticliy with Objective C. The appear in the Storyboard like this:
and are in the tree in the same hirgachy as the UIViewController Node.
You can take outlet of that view in respective view controller class as you you take outlet of view put in viewcontroller's default view.
Then in your viewDidload you can add that view to your default view!
For example your outlet is outterView then in viewDidload,
[self.view addSubview:self.outerView];
Second thing if you are adding view in viewDidload and you need your view's size as screen size than in viewDidappear you can do like,
self.outerView.frame = self.view.frame;
Ok it was my fault, sorry folks.
I also need an IBOutlet to the to the ViewControllers view. So connect the them in InterfaceBuilder first and give the View the the customClass.
Referencing Outlets
view->UIViewContoller
HelloUIClass *viewThis = [[HelloUIClass alloc] init];
[self.view addSubview:viewThis]
…this is a start not sure about that.
You just have to do it right in Interface Builder!
DRAG AND DROP! the Reference Outlet in to your Headerfile under the #interface.
Open both windows. The Storyboard and your Controller Class .h file.
Grap an REFERENCING OUTLET from the View in File Inspector or right Mouse click and draw line into your Sourcecode. If you have done your class properly it will hook under your #interface line. AfterDroping you have give it a name "myViewInIB" and than you have just something like this:
#interface UIMainView : UIViewController;
#property (weak, nonatomic) IBOutlet UICoustomView *myViewInIB;
than you can use it normaly in your Class (Obj C)
[self.view addSubview:self.myViewInIB]
you said you have class of that view so you can do like this
Suppose your class name is View then,
1) in storyboard give name "View1" of class to view
2) you need to create Class if you want to give size programatically
3) for view size you can also use constraints instead of frame.
View1 *objView = [[View1 alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
viewObj.center = self.view.center;
viewObj.backgroundColor = [UIColor redColor];//so you can find view easily
[self.view addSubview: objView];

Can I set a UIView hidden is true in the viewDidLoad()?

I have designed a UIView in storyboard and the parent view is a tableview. But I want to set it hidden or unhidden depend on my decision.How can I do?
Get the view by tag? Or any other method?
I put up the code:
UIView *myview=[[UIView alloc] viewWithTag:99];
myview.hidden=true;
But it does not work!
Give the IBOutlet to the UIView in storyboard
#property(nonatomic, weak)IBOutlet UIView *subView;
In the ViewDidLoad or ViewWillAppear
put this line
subView.hidden=YES;
You should replace this line UIView *myview=[[UIView alloc] viewWithTag:99];
with
UIView *myview = [self.view viewWithTag:99];
if you have added on the main view and then try setting the view hidden.
Try writing this code in viewDidAppear.

UIButton not clickable outside initial frame of an UIScrollView

I got stuck with the strange behavior of UIScrollView that I cannot click on the UIButton after I enlarge the contentSize of the UIScrollView.
What I wanted to do:
Show a form with using UIScrollView
After clicking on the submit button, the scroll view will enlarge some amount of height to show further information.
In the further information, I will place and show another UIButton(hidden at the beginning) in order to process to the next step.
The problem is that I placed the new UIButton to the enlarged area and the button is not clickable whereas I placed the UIButton to existing area(the initial frame of scroll view) then the button works normally. For both cases, the scroll bar of the scroll view performs normal behavior.(ie, the scroll end is the new content height)
Here is what I have so far:
A UIView xib (placed all elements inside it including the hidden button)
A UIScrollView (loaded the UIView xib into it)
UIView* view = [[[NSBundle mainBundle] loadNibNamed:#"view" owner:self options:nil] objectAtIndex:0];
[view loaded];
[scrollView addSubview:view];
After the submit button is clicked:
// offset = some amount;
[scrollview setContentSize:CGSizeMake(scrollview.contentSize.width, scrollview.contentSize.height+offset)];
// h = some amount before the end of the scroll view
CGRect r = nextBtn.frame;
r.origin.y = h;
nextBtn.frame = r;
[nextBtn setHidden:NO];
I have tried to change the clipSubviews attribute of scroll view but seems it is not working for my case at all.
Anyone knows how it happens? And is there any better methods to get this job done? (resize the scroll view and then place another button in it)
Any help would be great! Thanks a lot!
I assume that the button is direct or indirect child of the contents view you load from the xib file.
The problem is because you changed the scroll view's contentSize but did not change the frame of the contents view. Superviews participate in touch hit testing. You can place a subview outside of its superview bounds and by default the subview will not be clipped but touch events will not be delivered to it because the superview checks during hit testing that the touch is outside of its bounds and rejects the touch. See UIView hitTest:withEvent: for details.
You can add a button say submit and another button as subview of the scrollview.Keep your another button hidden in start and call setContentsize. now when user clicks on submit button then set another button to visible and again call set content size.In this way you would be able to achieve what you want to.
i have tried to generate scenario for you.I have tried this on view based application with storyboard without auto layout.
so in story board do some thing like this:
#import <UIKit/UIKit.h>
#interface ALViewController : UIViewController
#property (nonatomic, weak) IBOutlet UIScrollView *scroll;
#property (nonatomic, weak) IBOutlet UIButton *btn1;
#property (nonatomic, weak) IBOutlet UIButton *btn2;
===========
implementation:
#import "ALViewController.h"
#interface ALViewController ()
#end
#implementation ALViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.btn2 setHidden:YES];
[self adjustContentSize];
}
-(void)adjustContentSize
{
CGRect contentRect = CGRectZero;
for (UIView *view in self.scroll.subviews)
{
if (![view isHidden]) {
contentRect = CGRectUnion(contentRect, view.frame);
}
}
self.scroll.contentSize = contentRect.size;
}
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)btnClicked:(id)sender
{
UIButton *btn = (UIButton *)sender;
if (btn.tag ==1)
{
[self.btn2 setHidden:FALSE];
[self adjustContentSize];
}
}
#end
and in storyboard some thing like this:
#end
for your information:view frame 0,0,768,1024
scroll frame: 0,0,768,1024
btn1 frame: 361, 753, 46, 30
btn2 frame: 351, 1032, 46, 30
run your code :
at first your scroll view would be unscrollable because we have set btw 2 to be hidden and set content size.As soon as you will be clicking on btn 1, see code we are making btn 2 visible again and calling set content size.
Conclusion:
the scrollview content size looks for its visible contain of which we can take benefit of.
thanks and regards.

Orientating app's view to landscape and portrait

I've UIViewController and another UIView with some images and labels on it.
I wanna change orientation and it works well, I can change aqGridview frame when rotating device,
My code :
- (void)makePortraitOrientation {
[gridView setFrame:CGRectMake(0, 43, 768,961)]; //works like a charm
HeaderView* dsd = [[HeaderView alloc] initWithParentID:self]; // in this way there is no errors but nothing changes
[dsd.label setFrame:CGRectMake(0, 0, 0, 0)];
[dsd setNeedsDisplay];
[self.HeaderView.label setFrame:CGRectMake(0, 0, 0, 0)]; //error, no object HeaderView in UIViewController
[self.HeaderView.label CGRectMake]
}
Header file of UIView
#interface HeaderView : UIView{
id parentID;
UIButton *menu;
UIImageView *icon;
NSString *product;
THLabel *label;
NSMutableArray *backs;
}
The main problem is how can i change frames and places of images and
labels which is on another UIView, not in my UIViewController ?
All the views created programatically.
Based on your controls frame you need to adjust the position of our controls while rotating the device.
Access controls which is in our view controller like this. [self.button setFrame:];
Access the sub view controls frame like this: [self.subviewname.control setFrame:];
Based on the orientation you need to handle the frames. You can use auto re-sizing also.
Update:
Make sure whether your sub view is added properly in your view controller like this :
[self.view addSubView:self.mysubView];
Increase the score of your sub view(HeaderView) as well.

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

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

Resources