my paging scroll view is moving either horizontally and vertically and it should move only horizontally. I made an application which consists of 2 view the paging scroll view is a subview of one of them which frame is 320*400. my paging scroll view is 320*360 frame.i thought that it will move only horizontally. I pasted part of my code:
CGRect pagingScrollViewFrame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
self.view = pagingScrollView;
...
- (CGSize)contentSizeForPagingScrollView {
CGRect bounds = pagingScrollView.bounds;
return CGSizeMake(bounds.size.width * [self imageCount], bounds.size.height);
}
...
- (NSUInteger)imageCount {
static NSUInteger __count = NSNotFound;
if (__count == NSNotFound) {
__count = ([rootArray count]/5);
}
return __count;
}
thanks
Try to add
pagingScrollViewFrame.scrollsToTop=NO;
Hope it will help
Related
I am using the following code to scroll to the next page of a UITableView that has a scrollview. Scrolling works but the page the user lands to depends on the power he scrolls with his finger. Whatever the force of scrolling is it needs to land to the next page. How can i achieve this? Any help appreciated...
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint
*)targetContentOffset {
self.tableView.rowHeight = [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.frame.size.height-(self.appDelegate.heightOfEightFeedWalletTopConstraint*2)+12;
int tomove = ((int)targetContentOffset->y % (int)self.tableView.rowHeight);
if(tomove < self.tableView.rowHeight/6 || velocity.y < 0){
targetContentOffset->y -= tomove;
}
else{
targetContentOffset->y += (self.tableView.rowHeight-tomove);
}
}
You don't need any of that code...
All you need to do is enable paging on the scroll view.
This will
create a scroll view with 20-points on all 4 sides
add a vertical stack view
add 10 labels as arranged subviews (each label will be the height of the scroll view frame)
Scroll view has paging enabled:
#import <UIKit/UIKit.h>
#interface PageScrollViewController : UIViewController
{
UIScrollView *scrollView;
}
#end
#implementation PageScrollViewController
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [UIScrollView new];
[scrollView setPagingEnabled:YES];
UIStackView *stack = [UIStackView new];
stack.axis = UILayoutConstraintAxisVertical;
stack.distribution = UIStackViewDistributionFillEqually;
stack.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:stack];
[self.view addSubview:scrollView];
// respect safeArea
UILayoutGuide *g = self.view.safeAreaLayoutGuide;
UILayoutGuide *cg = scrollView.contentLayoutGuide;
[NSLayoutConstraint activateConstraints:#[
[scrollView.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
[scrollView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:20.0],
[scrollView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-20.0],
[scrollView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:-20.0],
[stack.topAnchor constraintEqualToAnchor:cg.topAnchor constant:0.0],
[stack.leadingAnchor constraintEqualToAnchor:cg.leadingAnchor constant:0.0],
[stack.trailingAnchor constraintEqualToAnchor:cg.trailingAnchor constant:0.0],
[stack.bottomAnchor constraintEqualToAnchor:cg.bottomAnchor constant:0.0],
[stack.widthAnchor constraintEqualToAnchor:scrollView.frameLayoutGuide.widthAnchor],
]];
// now let's add some views to the stack view
// making each one the same height as the scroll view frame
NSArray <UIColor *> *colors = #[
UIColor.yellowColor,
UIColor.greenColor,
UIColor.cyanColor,
UIColor.orangeColor,
];
for (int i = 0; i < 10; i++) {
UILabel *v = [UILabel new];
v.textAlignment = NSTextAlignmentCenter;
v.text = [NSString stringWithFormat:#"Label %d", i];
v.backgroundColor = colors[i % colors.count];
[stack addArrangedSubview:v];
[v.heightAnchor constraintEqualToAnchor:scrollView.frameLayoutGuide.heightAnchor].active = YES;
}
}
#end
I have a UISearchController just over my UITableView and my table has many recrods. When a user goes to bottom in the table, he has to come back to top to see the search bar. Below you can see how I create the search bar:
- (void) showSearchBar {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
//self.sharedNavigationItem.titleView = _searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
}
I was wondering how to show the search bar when I scroll down (go up) in the table and hide it back when I scroll up (go down) in the table even I am at the bottom of the table.
The search bar can be placed arbitrarily in the UI. Create a sibling view to the table view, placed -- for example -- above the table. Give that view a height constraint set initially to 44px and provide outlets to both the view and the constraint...
#property(weak,nonatomic) IBOutlet UIView *searchBarContainerView;
#property(weak,nonatomic) IBOutlet NSLayoutConstraint *searchBarHeightConstraint;
Now, your setup code changes to:
// ...
self.searchBarHeightConstraint.constant = self.searchController.searchBar.bounds.size.height;
[self.searchBarContainerView addSubview:self.searchController.searchBar];
// ...
If you make the header stay at top, then the search field should stay visible
UITableViewStylePlain: A plain table view. Any section headers or footers are displayed as inline separators and float when the table view is scrolled.
UITableViewStyleGrouped: A table view whose sections present distinct groups of rows. The section headers and footers do not float.
I trying to help you out with what I did, hope it helps you.
This is how I added my label in header view of table view, I tried to replace it by your search controller;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 40);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor clearColor];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
[view addSubview:_searchController];
view.tag = 123;
return view;
}
If you wish to hide effect at complete end of dragging move use below method;
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (velocity.y > 0){
NSLog(#"up");
[self.view viewWithTag:123].hidden = true;
}
if (velocity.y < 0){
NSLog(#"down");
[self.view viewWithTag:123].hidden = false;
}
And for immediate changes of scrolling the tableview use below method :
-(void)scrollViewDidScroll:(UIScrollView *)scrollView //working delegate
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
NSLog(#"down");
[self.view viewWithTag:123].hidden = false;
} else {
NSLog(#"up");
[self.view viewWithTag:123].hidden = true;
}
}
After some searching I thought of some new answer, hope it helps you!
To identify direction of Scroll:
BOOL showSearch;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
// down
showSearch = true;
} else {
// up
showSearch = false;
}
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (showSearch) {
[self addSearch];
}
else {
[self removeSearch];
}
[_tableView reloadData];
}
To add & remove SearchController:
-(void)addSearch {
if (!_searchController){
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.active = NO;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.delegate = self;
}
}
-(void)removeSearch {
_searchController.active = NO;
[_searchController removeFromParentViewController];
_searchController = nil;
_tableView.tableHeaderView = nil;
}
I have added a shadow to a UITableView (which covers a third of the screen sfrom the bottom - see attached screenshot) using the following in a UIView Category:
- (void) addShadow {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
self.layer.masksToBounds = NO;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 1;
self.layer.shadowOffset = CGSizeMake(-5,-5);
self.layer.shadowRadius = 20;
self.layer.shadowPath = path.CGPath;
self.layer.shouldRasterize = YES;
}
It appears as expected, but when I scroll it up, the shadow scrolls up too. Also, the table scrolls beyond its upper bound. Can you suggest what is wrong here? if I comment self.layer.masksToBounds = NO;, the shadow disappears, but the table scrolling is as expected. Hence, the problem lies somewhere around masksToBounds perhaps.
I solved it by putting an identical view underneath, just for the shadow. Not a clean solution ... hence I am still open to answers. My code is as follows:
- (UIView*) addShadow {
UIView* backView = [[UIView alloc] initWithFrame:self.frame];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:backView.bounds];
backView.layer.masksToBounds = NO;
backView.layer.shadowColor = [UIColor blackColor].CGColor;
backView.layer.shadowOpacity = 1;
backView.layer.shadowOffset = CGSizeMake(-5,-5);
backView.layer.shadowRadius = 20;
backView.layer.shadowPath = path.CGPath;
backView.layer.shouldRasterize = YES;
[self.superview addSubview:backView];
[self.superview bringSubviewToFront:self];
return backView;
}
(void) removeShadow {
self.layer.masksToBounds = YES;
self.layer.shadowColor = nil;
self.layer.shadowOpacity = 0;
self.layer.shadowOffset = CGSizeMake(0,0);
self.layer.shadowRadius = 0;
}
I tried to implement paging with 2 views from this tutorial:
Multiple virtual pages in a UIScrollView with just 2 child views
I wanted to modify this by putting UIScrollView to pages and in every page updating it with new content:
-(void)addContent:(NSString*)txt{
int size = 5;
UITextView *txtView;
int row = 0;
//from left
int xPlace = 0;
//space between row's
int rowSpace = 20;
//from top
int yPlace = 10;
// int button_with = scrollView.frame.size.width;
int button_height = 20;
for (int i = 0; i < size; i++) {
txtView = [[UITextView alloc] initWithFrame:CGRectMake(xPlace, row*rowSpace+yPlace, scrollView.frame.size.width, 2*button_height)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.backgroundColor = [UIColor blueColor];
[txtView setText:[NSString stringWithFormat:#"Numer %# - %i",txt,i]];
// answerCheck.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
[scrollView addSubview:txtView];
[txtView release];
row++;
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, txtView.frame.size.height*size)];
}
The idea is when scrolling just replace uiviewcontroller with new content. It calls:
- (void)setPageIndex:(NSInteger)newPageIndex
{
pageIndex = newPageIndex;
if (pageIndex >= 0 && pageIndex < [[DataSource sharedDataSource] numDataPages])
{
NSDictionary *pageData =
[[DataSource sharedDataSource] dataForPage:pageIndex];
label.text = #"Tekstas";//[pageData objectForKey:#"pageName"];
textView.text = [pageData objectForKey:#"pageText"];
[self addContent:[pageData objectForKey:#"pageText"]];
CGRect absoluteRect = [self.view.window
convertRect:textView.bounds
fromView:textView];
if (!self.view.window ||
!CGRectIntersectsRect(
CGRectInset(absoluteRect, TEXT_VIEW_PADDING, TEXT_VIEW_PADDING),
[self.view.window bounds]))
{
textViewNeedsUpdate = YES;
}
}
}
Well here I stuck. My text view is updating but new content of UIscrollView don't update in iPad simulator.
Yea I mean in iphone it works while in iPad it don't. What I could improve if I want to achieve this functionality?
Maybe there are other way to do paging from iOS 5?
I would put source that a tried to make working.
Download source
You can just use a nib file add a UIScrollView with fix width and height. Now make some pages as a UIView and add them to UIScrollView with same size and same origin. In the main UIView:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 2);
}
-(void)viewWillAppear:(BOOL)animated{
CGFloat h = scrollView.frame.size.height;
[pageView1 setFrame:CGRectMake(0, 0, pageView1.frame.size.width, pageView1.frame.size.height)];
[pageView2 setFrame:CGRectMake(0, h, pageView2.frame.size.width, pageView2.frame.size.height)];
}
And don't forget to check Paging Enabled for UIScrollView in the Attributes in the nib file.
I hope be useful for you!
I have a scrollView set up as the titleView of a navigationBar. The contentView of the scrollView has 4 buttons in it, all laid out dynamically. The buttons need to cancel with drags and scrolling, so I've subclassed them to setEnabled:NO on themselves when touchesMoved.
When I try to scroll the scrollView, as soon as I pass over the bounds of the superview (i.e., passing the bottom or top bounds navigationBar), the scrollview 'skips' 8 or 11 pixels, depending on if its the top or the button. It has something to do with the UIButtons, because when I just use UILabels the problem doesn't persist.
Relevant code:
Scrollview Initialization:
menuWidth = 196;
rowHeight = 40;
rowMargin = self.frame.size.height - rowHeight;
//drop down menu stuff
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.contentSize = CGSizeMake(self.frame.size.width, (self.frame.size.height)*choices.count);
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.clipsToBounds = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.delegate = self;
[self layoutMenu];
[self addSubview:scrollView];
[scrollView release];
Button initialization (-layoutMenu):
SKScrollableButton *tempButton;
NSString *text;
for (int i = 0; i < choices.count; i++) {
text = (NSString*)[choices objectAtIndex:i];
if (i == 0) {
text = [text stringByAppendingString:downArrow];
}
CGRect labelRect = CGRectMake(0, i*(rowHeight + rowMargin), menuWidth, rowHeight - 7);
tempButton = [[SKScrollableButton alloc] initWithFrame:labelRect];
[tempButton setTitle:text forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[tempButton addTarget:self action:#selector(buttonUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:tempButton];
[tempButton release];
}
No events get called on the buttons when the skipping happens, and only normal-seeming scrollViewDidScroll delegate calls happen on the scrollView.
I've tested and the skipping definitely happens when passing the bounds of the superview, not the bounds of the scrollView itself.