ios to create view programmatically - ios

I am trying to create a view like the below one programmatically but it covers the whole screen and does not be like this. There could be some thing size = Freeform, but could not find code on google to handle this.
UIView *picker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
picker.backgroundColor = [UIColor whiteColor];
Actually I want to create this whole view programmatically not though interface Builder.
please help

No other way than CGRectMake
if you want create the UIView that covers the whole screen
UIView *picker = [[UIView alloc] initWithFrame:self.view.bounds];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
if you want create the UIView that not covers the whole screen
UIView *picker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 260)];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];

Create UIToolBar which has UIBarButtonItem(=Done).
Set the UIToolBar as UIDatePicker's inputAccessoryView.
You don't even need a backgroundView to add Done button and date picker.

Related

bringSubviewToFront does not work on custom button's subview [duplicate]

I have a UIView in which I define it's border in the following manner:
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?
According to the Apple specification: It is composited above the receiver’s contents and sublayers.
So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.
So I make a background view to fake the border.
E.g.:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;
UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];
[backgroundView addSubview:bView];
[self.window addSubview:backgroundView];
and the effect:
Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.
Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.
Insert layer at specific position that suits you:
self.layer.insertSublayer(sublayer, at: 0)

Add a transparent view above an image view using objective c [duplicate]

This question already has answers here:
Replacing UINavigationControllers NavigationBar with UIView
(2 answers)
Closed 7 years ago.
i have a UIImageView .i want to add a trans
inCallBackGroundImgae = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"caller_texture.png"]];
[inCallBackGroundImgae setFrame:CGRectMake(0,top, self.view.frame.size.width, 250)];
[self.view addSubview: inCallBackGroundImgae];
i want to add a transparent view, where i add a red colour .i don,t want navigation bar i want to add a transparent view
Thanks in advance
Firstly hide navigation bar like this in viewDidLoad method
[self.navigationController.navigationBar setHidden:YES];
Now add your transparent UIView in place of navigationBar
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 64)];
transparentView.alpha = 0.33f;
[self.view addSubview: transparentView];
EDIT : Adjust your image view frame as transparent view will not be seen.
CGRect imgViewRect = inCallBackGroundImgae.frame;
imgViewRect.origin.y = 64;
imgViewRect.size.height = decrease height here if necessary
inCallBackGroundImgae.frame = imgViewRect;
Please do this one
UIView *transView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 50)];// set your height here what ever you wanna.
transView.alpha = 0.5f;// set your alpha how much you wanna transparent.
[inCallBackGroundImgae addSubview: transView];

Removing a UIView on connectionDidFinishLoading

I create a UIView inside the viewDidLoad event using the following code:
// add a white UIView
UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
whiteView.tag = 777;
whiteView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:whiteView];
This displays a white screen (view) to the user. Now, when the connection did finish loading the request I like to remove this view. I use the following code inside the connectionDidFinishLoading.
UIView *whiteView = [self.view viewWithTag:777];
[whiteView removeFromSuperview];
But for some reason the whiteView above is not getting removed and I see the white screen all the time. What am I doing wrong?

Subview isn't positioned correctly

I am adding a UIDatepicker to a UIViewController, which is the rootview of a UINavigationController, and I use this code to position the Datepicker at the bottom of the screen.
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 216, 320, 216)];
[self.view addSubview:picker];
This should normally place the picker exactly at the bottom of the screen, but it is 44p missplaced. The Navigationbar is exactly 44p heigh, so I think this is the problem, but I don't know why the frame height includes the height of the navbar, when it doesn't act like a Subview.
I know a simple way would be to substract 44, but I am looking for a solution without any fixed numbers. Is there a way to implement it and can someone please explain me why the view includes the height of the navbar?
set the translucent property of navigation bar to YES to solve your problem..
Ex:
self.navigationControllerInstance.navigationBar.translucent = YES;
Well I think that since your UIViewController is the root view of a UINavigationController it's implicitly including the navigationBar on its bounds.
To get its frame programmatically, you could use:
CGRect navframe = [[self.navigationController navigationBar] frame];
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 216, 320, 216)];
[self.view addSubview:picker];
try this!
It's better not to hardcode any dimensions. Try this code:
UIDatePicker *picker = [[UIDatePicker alloc] init];
CGRect pickerFrame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - CGRectGetHeight(picker.bounds), CGRectGetWidth(picker.bounds), CGRectGetHeight(picker.bounds));
[picker setFrame:pickerFrame];
[self.view addSubview:picker];
It works and does not assume that a datepickers height is always 216. That may change in future iOS implementations, so it is better to check the size at runtime.
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I guess this will solve your problem!

Add Subview above mainviews layer

I am having trouble adding a subview above the CALayer of its parent view.
I tried to attach an image to show what i mean but i dont have enough reputation so here is a link to the image:
http://imageshack.us/photo/my-images/843/img0219u.png/
Part of the subview is obscured by the parent views border.
How can i make the subview appear over the top of the parent views layer?
Any help is appreciated.
Here is my code if it helps:
Code for adding layer:
UIView *student = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
student.backgroundColor = [UIColor grayColor];
CALayer *studentLayer = [[CALayer alloc] init];
studentLayer.borderColor = [UIColor grayColor].CGColor;
studentLayer.borderWidth = 5;
student.layer.borderColor = studentLayer.borderColor;
student.layer.borderWidth = studentLayer.borderWidth;
Code for adding subview:
UILabel *ilp = [[UILabel alloc] initWithFrame:(CGRectMake(-20, -10, 40, 20))];
ilp.text = #"ILP";
ilp.backgroundColor = [UIColor yellowColor];
ilp.textColor = [UIColor blackColor];
ilp.textAlignment = NSTextAlignmentCenter;
[student addSubview:ilp];
Thats not possible so far, The best solution i can think of is that add both the subview and its parent view inside another view than its just a matter of reordering two subviews, so that your subview is above the other and its border.
Hope that help!

Resources