uiscrollview strange offset - ios

I have a issue that my viewDidLayoutSubviews creates a strange offset between my table wiche is right under the view and my view
I for example uncomment this section of my code and scroll in my table this content view diapers as soon as I start to scroll
looks like this:
Offset between view and table
I really do not have a clue why this exists. The problem is not regarding the constrains I have tested it with connecting the view with the code and not.
What I do
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self.TableView setContentInset:UIEdgeInsetsMake(self.ContainerView.frame.size.height, 0.f, 0.f, 0.f)];
[self.TableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
float headerImageYOffset = 88 + self.ContainerView.bounds.size.height - self.view.bounds.size.height;
CGRect headerImageFrame = _ContainerView.frame;
headerImageFrame.origin.y = headerImageYOffset;
}
What I do when I scroll
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scrollOffset =-scrollView.contentOffset.y;
CGFloat yPos = scrollOffset - _obergurglContainerView.bounds.size.height;
_obergurglContainerView.frame = CGRectMake(0, yPos, _obergurglContainerView.frame.size.width, _obergurglContainerView.frame.size.height);
float Alpha = 1.0-(-yPos/_obergurglContainerView.frame.size.height);
_obergurglTitleImageView.alpha = Alpha;
NSLog(#"THE Alpha Value : %f", Alpha);
NSLog(#"The off set of the scroll view is %f",scrollOffset);
}
Thanks for help and fast answer ! :)

So reading again the question I figured out about your problem.
I think you shouldn't use viewDidLayoutSubviews.
viewDidLayoutSubviews is called all the time while you are scrolling, which is modifying the inset in your tableview all the time.
try to place
[self.TableView setContentInset:UIEdgeInsetsMake(self.ContainerView.frame.size.height, 0.f, 0.f, 0.f)];
where you create your tableview.

Related

fade/show UIButton when scroll down/up

I have a tableView. on view is UIButton. i want when scroll down button fade and when scroll stop or up button show.
code:
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(#"Start scroll");
//I assume the buttons are within your cells so you will have to enable them within your cells so you will probably have to handle this by sending a custom message to your cells or accessing them with properties.
CGRect frame = addCommentBtn.frame;
frame.origin.y = scrollView.contentOffset.y + self.newsTableView.frame.size.height + 4;
addCommentBtn.frame = frame;
[self.view bringSubviewToFront:addCommentBtn];}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(#"End scroll");
// do the same to enable them back
CGRect frame = addCommentBtn.frame;
frame.origin.y = scrollView.contentOffset.y + self.newsTableView.frame.size.height + 4;
addCommentBtn.frame = frame;
[self.view bringSubviewToFront:addCommentBtn];}
but don't work correctly!
Thanks
You are using wrong method. Instead of
scrollViewWillBeginDecelerating: use scrollViewDidScroll: and update button alpha and position based on contentOffset of the scroll view.

Implementing iPad General Setting Page

So far I have customized the tableview and implemented the iPad General Setting Page. Below is the code for tableview which will change frame accordingly. But the issue is when I insert/delete rows or section in the tableview. My tableviewcell backgroundview (not cell) width get shrinks. Any idea what wrong am I doing here?
- (void)setFrame:(CGRect)frame
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
CGFloat inset =10;
frame.origin.x += inset;
frame.size.width -= 2 * inset;//The issue is in this line
}
[super setFrame:frame];
}
I found the simple solution to achieve this. No need of customizing UITableView subclass. Just take the outlet of tableview and set the frame and change the color of backgroundview. like that below:-
CGFloat tableBorderLeft = 10;
CGFloat tableBorderRight = 10;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
yourTableView.frame = tableRect;
self.view.backgroundColor=[UIColor colorWithRed:(239/255.0f) green:(239/255.0f) blue:(244/255.0f) alpha:1.0];

Scroll UITableView Cell beyond table bounds

I have a UITableView with changing height.
I wanted in every cell selection to kind of get the 'focus' on it by moving it to the middle of the screen. The problem is to move the upper and lower cells in the table since they cannot scroll beyond the table upper and lower bounds. I'll also prefer to do this with animations..
Any suggestions?
Thanks!
I created small example of how to achieve this:
https://github.com/vkozlovskyi/CenterCellExample
You just need to calculate scroll offset of UITableView, and and animate the change:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get offset for cell
CGFloat centerOffset = self.tableView.frame.size.height / 2 - self.tableView.rowHeight / 2;
// Content offset
CGFloat total = indexPath.row * self.tableView.rowHeight;
[UIView animateWithDuration:0.3 animations:^{
self.tableView.contentOffset = CGPointMake(0, total - centerOffset);
}];
}
It works for all cells, upper and lower too.
You can add extra space to the content of your table view using the contentInset property. Add extra space above and below your table view will allow your cells to scroll all the way to the middle of your table view.
UITableView * tableView = self.tableView ;
CGRect bounds = self.tableView.bounds ;
CGFloat extraSpaceNeeded = 0.5 * ( bounds.size.height - tableView.rowHeight ) ;
tableView.contentInset = (UIEdgeInsets){ .top = -extraSpaceNeeded, .bottom = -extraSpaceNeeded } ;
Now you can just use
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:animated];

Rect UITableViewCell in iOS 8 when beginupdate and endupdate

I'm building a UITableView with style tableview Grouped, and i give a gap (margin left) on method frame UITableViewCell like this
- (void)setFrame:(CGRect)frame {
if(IOS_7){ //above ios 6
CGFloat gap = 10.0f;
frame.origin.x += gap;
frame.size.width -= gap*2;
}
[super setFrame:frame];
}
the result like this.
and then after 5s, i did call method
[_table beginUpdates];
[_table endUpdates];
the result like this.
the gap on the right twice from my gap = (gap*2)
this problem only on ios 8,
is the iOS 8 call twice setFrame in uitableviewcell ?
and how to solve it ?
note:
- i have been tried with reset frame with CGRectMake(0.0f, frame.origin.y, [UIScreen mainScreen].bounds.size.width, frame.size.height); but if i'm using with ipad with left menu side, it's not solve my problem,
- does not use custom cell (just using default uitableviewcell)
You are setting the frame wrong. You should be doing this:
- (void)setFrame:(CGRect)frame {
if(IOS_7){ //above ios 6
CGFloat gap = 10.0f;
frame.origin.x += gap;
frame.size.width -= gap*2;
[super setFrame: frame];
}
}

How to scroll to a certain point in a UIScrollView

Below I have a line of code that scrolls to a certain point in a UIScrollView. However once it has scrolled there it does not allow me to scroll back up.
[scroller setContentOffset:CGPointMake(0,500) animated:NO];
Any help will be appreciated, thank you.
did you try using UIScrollView method scrollRectToVisible:
Here is an example
- (void)goToPage:(int)page animated:(BOOL)animated
{
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
[self.scroller scrollRectToVisible:frame animated:animated];
// update the scroll view to the appropriate page
}
I use this. The button calls this method. Should work well.
- (void)downExecute:(id)sender {
NSLog(#"scroll down");
CGFloat currentOffset = _scrollView.contentOffset.y;
CGFloat newOffset;
newOffset = currentOffset + self.view.bounds.size.height;
[UIScrollView animateWithDuration:0.3 animations:^(void) {
[_scrollView setContentOffset:CGPointMake(0.0, newOffset)];
} completion:^(BOOL finished) {
}];
}
I had this kind of problem because I called this method from viewDidLayoutSubviews(). Check where are you calling it from. When I moved the logic to viewDidAppear() everything worked as expected. Hope it helps, cheers.

Resources