Set position of UIPickerView at bottom even after scroll in ios - ios

I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:
-(void) showPickerView {
[self.view resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.

Solution 1:
Add your pickerview to your window.
Solution 2:
Implement the scrollview delegate and adjust the frame continuously.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
}

I can just suppose that you are showing your picker inside of a scroll view, right?
And if so, then your picker frame is relative to UIScrollView, not to a screen view.
So you have to take into account your scroll view offset. Something like that:
CGRect frame = self.picker.frame;
frame.origin.y = self.scrollvew.contentOffset.y + 270;
Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).

Related

UIScrollView doesn't scroll with method scrollRectToVisible

I have a question for you!
I have a method:
- (void)pushNewTableViewController:(NewTableViewController *)newTableViewController
{
[self addChildViewController:newTableViewController];
[self.scrollView addSubview:newTableViewController.view];
[UIView animateWithDuration:0.5f animations:^
{
CGRect frame = CGRectZero;
frame.origin.x = COLUMN_WIDTH * self.navigationStack.count;
frame.origin.y = 0.0f;
frame.size.width = COLUMN_WIDTH;
frame.size.height = self.view.frame.size.height;
newTableViewController.view.frame = frame;
}
completion:^(BOOL finished)
{
[self.navigationStack addObject:newTableViewController];
self.scrollView.contentSize = [self sizeForContent];
[self.scrollView scrollRectToVisible:newTableViewController.view.frame animated:YES];
}];
}
Basically, this method works fine, except the line [self.scrollView scrollRectToVisible:newTableViewController.view.frame animated:YES];.
The idea of this method is to scroll the scroll view to the right to make newTableViewController.view.frame to be fully visible. But instead of this, scroll view doesn't scroll anywhere...
Maybe any one of you, guys, know why it doesn't work as I expect?

view frame does not move down 20px (height of the statusbar)

I used the code below to present vViewController1
#property (retain,nonatomic) ViewController1 *vViewController1;
...
push vViewController1 from rootViewController
[self.navigationController pushViewController:vViewController1 animated:NO];
Viewcontroller1's viewWillAppear
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect screen = [[UIScreen mainScreen] bounds];
if (self.navigationController) {
CGRect frame = self.navigationController.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.navigationController.view.frame = frame;
} else {
if ([self respondsToSelector: #selector(containerView)]) {
UIView *containerView = (UIView *)[self performSelector: #selector(containerView)];
CGRect frame = containerView.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
containerView.frame = frame;
} else {
CGRect frame = self.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.view.frame = frame;
}
}
}
}
but it displays as below and does not move down 20px(height of the statusbar)
Your comment welcome
Assuming you are NOT using Autolayout (seeing as you are playing with frame sizes) then in Storyboard select the ViewController and then goto the Size Inspector (little ruler) and then adjust the Y delta for the view (change to 20 or -20 depending on what mode you are "View As").
This may help here although I am not sure about the 18 value in Y I always thought it was 20!

Programmatically change position of UIImage

I tried to create a simple animation on an UIImage that has to start from the position X = 0 and has to end at the position X = device width
I tried to use the following code inside the viewDidLoad method:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
//CGFloat screenHeight = screenSize.height;
for(int x = 0; x < screenWidth; x++)
{
[_camion setFrame:CGRectMake(x, _camion.frame.origin.y, _camion.frame.size.width, _camion.frame.size.height)];
}
But the image doesn't move, am I missing something?
For animations there are better methods instead of a for loop
I try to give you an example:
-(void)viewDidAppear:(BOOL)animated {
CGFloat widthView = self.view.frame.size.width;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
_camion.frame = CGRectMake(widthView, _camion.frame.origin.y, _camion.frame.size.width, _camion.frame.size.height);
}completion:^(BOOL finished){
NSLog(#"animation done!");
}];
}
With animateWithDuration you can set duration, delay and type (in this case UIViewAnimationOptionCurveEaseIn) of animation, so you can have more and better control over the end result.

UIWebView background image frame

This is my code
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height == 568) {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]];
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 568;
webview.frame = frame;
}
else {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting.png"]];
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 480;
webview.frame = frame;
}
}
else {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaitingipad.png"]];
CGRect frame = webview.frame;
frame.size.width = 768;
frame.size.height = 1024;
webview.frame = frame;
}
but when the page loads, the image is too big or too small, and it's not scaled depending on the device.
Can you help me?
I believe +colorWithPatternImage sizes the image to the frame at the time you call it, so resizing the frame after calling webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]]; would have no effect on scaling the image. Try setting the background image after you set the frame size. For instance:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 568;
webview.frame = frame;
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]];
}

UIKeyboardWillShowNotification incorrectly changes frame of UIWebView

I am using a UIKeyboardWillShowNotification to know when the keyboard is shown and adjust the size of my UIWebView so that it isn't hidden behind the keyboard.
The strange thing is, when I change the frame in the method that gets called by NSNotificationCenter it changes the frame in a way that lets me scroll my UIWebView content (red in screenshot), but also a large portion of the UIWebView scrolls into view (yellow in screenshot). The yellow should never be shown.
- (void)keyboardWillShowOrHide:(NSNotification *)notification {
// User Info
NSDictionary *info = notification.userInfo;
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
int curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboard = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
CGRect frame = self.toolbarHolder.frame;
frame.origin.y = (self.view.frame.size.height - keyboard.size.height) - 44;
self.toolbarHolder.frame = frame;
// Editor View
CGRect editorFrame = self.editorView.frame;
editorFrame.size.height = (self.view.frame.size.height - keyboard.size.height) - 44;
self.editorView.frame = editorFrame;
} completion:nil];
} else {
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
CGRect frame = self.toolbarHolder.frame;
frame.origin.y = self.view.frame.size.height;
self.toolbarHolder.frame = frame;
// Editor View
CGRect editorFrame = self.editorView.frame;
editorFrame.size.height = self.view.frame.size.height;
self.editorView.frame = editorFrame;
} completion:nil];
}
}
If I change the UIWebView frame in a different method than the one called from NSNotificationCenter, the frame changes correctly and the area above the keyboard is only filled with my HTML content within the UIWebView (red).
What could be causing this issue?
Use UIKeyboardFrameEndUserInfoKey key that returns the final expected frame for keyboard

Resources