Subview isn't positioned correctly - ios

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!

Related

My button not appears to correct position when viewController is refreshed

I am creating a simple UIButton programmatically with this frame size
UIButton *pickmeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 100, self.view.bounds.size.width, 60)];
it display at perfect place where i want
But on some action i want to reload the whole view controller, when i do it display the button at wrong coordinates
First, i am unable to find the reason for it. Second, what i think according to coordinates i have defined i think the second image is rendering the correct coordinates because, placement on Y coord is (self.view.bounds.size.height - 100) and height is 60. But when i make it -60 it starts displaying button below bottom line and very little of button shows up above.
Kindly share your views and solution on this.
Set AutoresizingMask for button
UIButton *pickmeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 60, self.view.bounds.size.width, 60)];
[pickmeButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
Please try with this.
UIButton *pickmeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 60, [UIScreen mainScreen].bounds.size.width, 60)];

UIViews moving after segue and return

I have an app in which I am creating a two views stacked on top of each other:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height -200)];
[self.view addSubview:headerView];
[self.view addSubview:bottomView];
[headerView setBackgroundColor:[UIColor lightGrayColor]];
[bottomView setBackgroundColor:[UIColor redColor]];
I also use a sidemenu which uses:
[self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"welcomeScreen"]]
animated:YES];
[self.sideMenuViewController hideMenuViewController];
The initial load is fine and both top and bottom views appear as expected, however, when I navigate away using the side menu and then navigate back to home using the sidemenu, both views appear to move up the page (behind the navigation bar).
I have tried everything I can think from programmatic constraints to checking that the Navigation bar is present and moving the code from viewDidLoad to viewDidAppear but I always get the same result. Any ideas as to what is happening and why? And any suggestions on a fix would be greatly appreciated. Thanks
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Has fixed the issue!

UIScrollView is not scrolling even with setting contentSize

My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.
I have a container view inside the scroll view that has a width of 960 points.
At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.
This is my viewDidLoad method:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView.scrollEnabled = YES;
[self.scrollView setFrame:CGRectMake(0,0,320,300)];
self.scrollView.contentSize = CGSizeMake(960, 300);
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
[containerView addSubview:subview1];
[self.scrollView addSubview:containerView];
}
Here is a sample code for create a scroll view programmatically:
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];
[scrollView addSubview:containerView];
So, there is no issue in your code, but something wrong with your storyboard configure.
I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.
Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?
If you can give me more information about this issue, I can help you.

ios to create view programmatically

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.

Add UISlider To UINavigationBar

Trying to add a UISlider to UINavigationBar. I have it to where the slider shows, but when trying to change the value, it will not move. Here is the code in viewWillAppear. Note that this is on a child view and not the main parent view.
UIView *viewofslider = [[UIView alloc] initWithFrame:CGRectMake(160, 10, 40, 30)];
UISlider *theslider = [[UISlider alloc] init];
[theslider addTarget:self action:#selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[theslider setBackgroundColor:[UIColor clearColor]];
theslider.minimumValue = 50.0;
theslider.maximumValue = 150.0;
theslider.continuous = YES;
theslider.value = 100.0;
[viewofslider addSubview:theslider];
[self.navigationController.navigationBar addSubview:viewofslider];
To answer your specific question, I think the width of viewofslider is too narrow at 40.0, so the touch is not being picked up by the UISlider.
If you change the width of viewofslider to 100.0 you should be able to interact with it.
However, I agree with rmaddy's comment that you should simply add the slider to the navigation bar, there's no obvious need for the UIView.
Try to add it to titleView of view controller's navigation item in viewWillAppear:
UISlider *theslider = [[UISlider alloc] init];
...
self.navigationItem.titleView = theslider;

Resources