I am trying to move my UIScrollView to make way for the keyboard when the keyboard is shown.
What's weird is the scrollview is moving too much. I noticed that if I even just set the content offset of the scrollview to (0,0) it still moves the view down. What's bizarre, is the contentOffset of the scrollview is 0,0 before I set it! Whattt!
- (void)keyboardWasShown:(NSNotification*)notification {
NSLog(#"%f %f", scrollView.contentOffset.x, scrollView.contentOffset.x);
[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
}
This prints (0,0) but if I comment the second line, the view doesn't move. Does setContentOffset have weird secondary consequences?
Try this:
- (void)keyboardWasShown:(NSNotification*)notification {
CGFloat ay = 150;
[scrollView setContentOffset:CGPointMake(0.0, textField.frame.origin.y-ay) animated:YES];
}
Here change the value of ay as per your requirement. Depends on how much you want the scrollview to move.
Related
I am using UIScrollview and set it's content size same as view's height.
I need to make my scrollview scrolls same in both side (Up / Down).
It will come back in same position I mean whenever we scrolls in top direction and leave scrolling it will come y = 20 pixels so when we scroll in bottom direction it will come back on the 20 pixels when we leave scrolling...
For setting scrollview content size
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollViewBg setContentSize:scrollViewContentSize];
Also I have tried to give content offset
[scrollViewBg setContentOffset:CGPointMake(0, 0) animated:YES];
but not solved my problem.
I just updated and make scrollview's height similar to view's height and increase it's content height like this and it solved my problem.
-(void)viewDidAppear:(BOOL)animated{
float space = 2.5; // This is for making scroll view scrolls and on same positions from both sides.
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width,
self.view.frame.size.height+space);
[self.scrollViewBg setContentSize:scrollViewContentSize];
[scrollViewBg setContentOffset:CGPointMake(0, 0) animated:YES];
}
I have a UITableView with a UIView on top. I want the UIView to stick to the top as the tableView cells scroll over it.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (self.tableView.contentOffset.y > 0) {
CGRect newframe = self.publicTopView.frame;
newframe.origin.y = -self.tableView.contentOffset.y;
self.publicTopView.frame = newframe;
NSLog(#"After: %f", self.publicTopView.frame.origin.y);
}
}
You need to set your table view header view to the view you want on top.
Add this code to you viewDidLoad
self.tableView.tableHeaderView = self.publicTopView
I'm not certain what you're trying to accomplish, but I have a guess at what is wrong. As you scroll your contentOffset will continue to change and let's say your tableView has a content size height of 1500, then your contentOffset will eventually be larger than the height of your view controllers view. Now see that you are putting that contentOffset into the origin.y of your publicTopView. So your publicTopView could possibly be moving too much, even offscreen depending on how large your tableview's content size is.
right now I'm trying to move up and move down an UIView based on roll up or down in UITableView. So, if I rolled up the view, which means indexPath.row will keep incremented; the view will appear slide down until certain position and stopped. Then when I rolled down the table view, which means indexPath.row, decremented; the view will disappear by slide up outside the display view.
Do anyone have any idea what I need to code? Right now I'm just simply able to hide and unhide the view. I need the code for animate it slide up and down.
mark=indexPath.row;
flag=flag+1;
if (flag-mark>1) {
postBox.hidden=NO;
flag=indexPath.row;
}
else
postBox.hidden=YES;
Thanks in advance for your help ^^
If you want to move the view according to the movement of the UITableView, you can calculate the content offset of the UITableView and apply it to your moving view. Declare a ivar of type CGFloat named lastOffset.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat distance = scrollView.contentOffset.y - lastOffset;
lastOffset = scrollView.contentOffset.y;
[UIView animateWithDuration:0.2 animations:^{
self.movingView.frame = CGRectOffset(self.movingView.frame, 0.0f, distance);
}];
}
I have a UITableView and its UITableViewCells can independently change their sizes via the function tableView:heightForRowAtIndexPath. They store UIImages of varying sizes. When I tap an image the keyboard is shown (lets say it is for tagging the images).
When I have, lets say, 3 cells (images) with a total height of 1 pixel less than the screen size it is all good. The keyboard is shown and I can scroll to see all the cells (images).
But when I add one more, pushing the total size to be more than the screen, it all breaks. The keyboard seems to be shown (I have an extra button on top, making it some pixels higher than the original keyboard, that is shown). But I cannot write anything. The button on top of the keyboard is to resign the keyboard, and it does not work. It is kind of stuck.
The project can be downloaded here:https://github.com/TokyoBirdy/cecilia/tree/master/Test (excuse the messy code)
I was able to solve my problem of the locked scrolling by removing the code that edits the view's origin. In addition, I implemented scrolling to the bottom cell by using the tableview's contentSize property in my calculations.
-(void) keyboardWillShow:(NSNotification *)note
{
if(!isKeyboardShowing)
{
isKeyboardShowing = YES;
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
CGFloat keyboardHeight = keyboardBounds.size.height;
CGRect frame = self.view.frame;
frame.size.height += keyboardHeight;
self.view.frame = frame;
CGPoint scrollPoint = frame.origin;
scrollPoint.y += _tableView.contentSize.height - keyboardHeight;
[_tableView setContentOffset:scrollPoint animated:YES];
}
}
I have implemented an UIScrollView delegate as:
- (void) scrollViewDidScroll: (UIScrollView *) scrollView {
CGRect bounds = scrollView.bounds ;
CGPoint scrollLoc = scrollView.contentOffset ;
NSLog(#"bounds: %# offset:%#"
, NSStringFromCGRect(bounds)
, NSStringFromCGPoint(scrollLoc)) ;
}
And whatever I do, scrolling or rotating the device, it seems that contentOffset and bounds.origin are always the same.
Why do we need a contentOffset if that is the same as the bounds origin, or what is the case when both are actually different?
According to the docs:
The contentOffset property is always the current location of the
top-left corner of the scroll bounds, whether scrolling is in progress
or not.
The View is at the same location in both case, but the content may be moved within the view if you use a ContentOffset.
You will find usefull information in episode 9.Image View ... of the Standford University series :
http://itunes.apple.com/fr/itunes-u/developing-apps-for-ios-hd/id395605774
As far as I know, they both offer you the same information, one as a rectangle with his width and height and the other just the origin point.