self.refreshControl = [[UIRefreshControl alloc]init];
[self.objDiscussiontopic addSubview:self.refreshControl];
[self.refreshControl addTarget:self action:#selector(refreshTable) forControlEvents:UIControlEventValueChanged];
- (void)refreshTable {
//TODO: refresh your data
[self.refreshControl endRefreshing];
}
This is the code i am using to add refresh controller in my scroll view.
But when i pull down the scroll view the refresh control is not showing and the targeted method is also not called why ?
I am searching this for a while but didn't get any proper answer .
One more thing scrollview having VFL Constraint some thing like this :
//Pin scrolview to Parent View
[VFLConstraint allignSubViewViewHorizentallyWithSubView:self.objDiscussiontopic OverSuperView:[self view]];
[VFLConstraint allignSubViewViewVerticallyWithSubView:self.objDiscussiontopic OverSuperView:[self view] WithTopPading:[CommonFunction convertIphone6ToIphone5:0] AndBotomPading:[CommonFunction convertIphone6ToIphone5:57]];
+ (void)allignSubViewViewHorizentallyWithSubView:(UIView *)subView OverSuperView:(UIView *)superView{
NSDictionary *viewDict= NSDictionaryOfVariableBindings(subView);
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[subView]|" options:0 metrics: 0 views:viewDict]];
}
+ (void)allignSubViewViewVerticallyWithSubView:(UIView *)subView OverSuperView:(UIView *)superView WithTopPading:(float)topPading AndBotomPading:(float)bottomPading{
NSDictionary *viewDict= NSDictionaryOfVariableBindings(subView);
NSDictionary *metrics=[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:topPading],#"topPading",[NSNumber numberWithFloat:bottomPading],#"bottomPading", nil];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-topPading-[subView]-bottomPading-|" options:0 metrics: metrics views:viewDict]];
}
Try to:
[self.scrollView insertSubview:self.refreshControl atIndex:0];
Check the contentSize it can be less that frame size, if so use this line of code:
self.scrollView.alwaysBounceVertical = true
I don't think a UIRefreshControl is designed to work with plain scroll views. Apple's docs specifically refer to UITableViews when talking about refresh control.
You can look at third party pull to refresh libraries to add this to UIScrollView
Related
I have 2 view controllers embedded in a view vertically. View controller A (uploader), and B (docList).
B contains a UICollectionView
All, methods except cellForItemAtIndexPath inside the datasource of the collection view get called correctly, and i double checked everything. There is 1 section. There are more than 0 rows. The size of the rows I return is smaller than the collection view, etc
Here's a diagram to illustrate the setup:
My issue is:
Unless i turn on setTranslatesAutoresizingMaskIntoConstraints to YES, cellForItemAtIndexPath will never be called. If i set that property to YES on the View Controller B's view, then it does get called. But the layout is then screwed up, because I am not using springs and struts. We only use constraints here.
Do you know what i can be doing wrong when embedding the view controller that contains the UICollectionView?
Here is the code that embeds the two view controllers' views, and sets them as child controllers:
- (MFFormBaseCell *)cellForComponent
{
self.cell = [[MFFormBaseCell alloc] initWithFrame:CGRectZero];
[self.cell addSubview: uploader.view];
[self.cell addSubview: docList.view];
UIView* uploaderView = uploader.view;
UIView* docListView = docList.view;
NSMutableArray* tempConstraints = [[NSMutableArray alloc]init];
[tempConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: #"V:|-8-[uploaderView]-1-[docListView]-8-|"
options: NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views: NSDictionaryOfVariableBindings(uploaderView, docListView)]];
[tempConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: #"H:|-[uploaderView]-|"
options: NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views: NSDictionaryOfVariableBindings(uploaderView)]];
[tempConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: #"H:|-[docListView]|"
options: NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views: NSDictionaryOfVariableBindings(docListView)]];
uploaderConstraints = [tempConstraints copy];
[self.cell addConstraints: uploaderConstraints];
[self.embedder addChildViewController:uploader];
[uploader didMoveToParentViewController:self.embedder];
[self.embedder addChildViewController:docList];
[docList didMoveToParentViewController:self.embedder];
docList.view.frame = self.cell.bounds;
return self.cell;
}
And here is the code from View Controller B, that sets up the UICollectionView and a vertical flow layout for it.
- (void)modelDidLoad
{
_dataSource = [[MFCardDataSource alloc] initWithData: self.cardModel];;
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:aFlowLayout];
[_collectionView setDelegate: self];
[_collectionView setDataSource:_dataSource];
[_collectionView setBackgroundColor:[UIColor clearColor]];
for (NSString* type in [MFCardCollectionModel typeArray])
[_collectionView registerClass:[MFImageCard class] forCellWithReuseIdentifier: type];
[_collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_collectionView];
[self registerConstraintsForView:_collectionView];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
[super modelDidLoad];
}
And the contents of registerConstraintsForView:
-(void) registerConstraintsForView:(UIView*)collectionView
{
NSDictionary* metrics = #{ #"padding": #PADDING };
NSDictionary* views = NSDictionaryOfVariableBindings(_collectionView);
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-padding-[_collectionView]-padding-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-padding-[_collectionView]-padding-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:metrics
views:views]];
}
I got around the problem by Subclassing UICollectionView, and using the subclass instead.
On the subclass I overrode 2 methods:
- (void) setContentSize:(CGSize)contentSize
{
CGSize origSize = self.contentSize;
[super setContentSize:contentSize];
if (!CGSizeEqualToSize(contentSize, origSize))
{
[self invalidateIntrinsicContentSize];
}
}
- (CGSize) intrinsicContentSize
{
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}
Then, i have a method inside my view controller which contains the Collection View, which calculates the required Height needed for the collection view.
I then call setContentSize with the calculated height on my subclass of Collection View, and that makes sure that it returns an intrinsicContentSize as tall as it needs to be to show all the card records inside it.
my application needs help sceen on top of the home view controller.I have taken the tabbar controller as a root view controller and added navigation view controller .So in appearance of the first screen both tabbar and navigation bar appears along with view body.When i tried to place the help screens by pageviewcontroller examples at the time of view did load in the first screen tab view and navigation bar are coming front keeping the page view controller back .i have tried
[self addChildViewController:walkthrough];
[self.view addSubview:walkthrough.view];
[self.tabBarController.view bringSubviewToFront:walkthrough.view];
please help how to get the page view screen front.And is there any examples tutorial please help me
Note:Botn navigation bar and tabbar should not be hide
This is what I have done for walkthrough in my application. You can download this FXBlurView library from here:
https://github.com/nicklockwood/FXBlurView
In my .h file
#import "FXBlurView.h"
IBOutlet UIView *viewWalkThrough;
FXBlurView *overlayView;
In my .m file
#define SYSTEM_SCREEN_SIZE [[UIScreen mainScreen] bounds].size
#define kProfileWalkthrough #"ProfileWalkthrough"
if (![[[NSUserDefaults standardUserDefaults] valueForKey:kProfileWalkthrough] boolValue]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kProfileWalkthrough];
[[NSUserDefaults standardUserDefaults] synchronize];
overlayView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, SYSTEM_SCREEN_SIZE.width, SYSTEM_SCREEN_SIZE.height)];
[overlayView setDynamic:YES];
[overlayView setBlurRadius:5.0f];
[overlayView setBlurEnabled:YES];
[overlayView setTintColor:[UIColor blackColor]];
[self.view.window addSubview:overlayView];
[self displayOverlay:viewWalkThrough aboveView:self.view.window];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeWalkThrough:)];
[viewWalkThrough addGestureRecognizer:tapGesture];
}
- (void)closeWalkThrough:(UITapGestureRecognizer *)tapRecognizer {
[overlayView removeFromSuperview];
[self.viewWalkThrough removeFromSuperview];
}
- (void)displayOverlay:(UIView *)subView aboveView:(UIView *)superView {
subView.translatesAutoresizingMaskIntoConstraints = NO;
[superView addSubview:subView];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[subView]|" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(subView)]];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[subView]|" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(subView)]];
[superView layoutSubviews];
}
I have a UICollectionView inside of a UITableViewCell, and the CollectionView cells show images that a user stores in the app. When the user taps the cell, the image is presented full screen. So I created a UIViewController, added a UIScrollView to it for zooming, then added a UIImageView to the ScrollView. Everything works nicely until the user changes the orientation of the phone. When they change to landscape the view adjusts but the scrollview and image view do not adjust, and therefore just show on the left side of the screen; they seem to keep the same bounds. I tried to fix this by following apple's tech note using the pure layout approach and adding constraints. But when I do this, the image that is displayed is zoomed all the way in and I cannot get it to fit correctly. When I use an NSLog statement to determine the frames, they are all set to self.view.frame and therefore printout the size of the screen, but the image still shows at full size. Everything is created programmatically, no xib files.
I would love to avoid the constraints, but they seem necessary for the user to be able to see images correctly in landscape mode. I have tried setting the contentSize of the scrollview, setting clipsToBounds to YES and changing the contentMode but none of these things have any effect. Any help is greatly appreciated!
collectionView:didSelectItemAtIndexPath:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *viewsDictionary;
//creating ViewController to be presented
UIViewController *fullImageView = [[UIViewController alloc] init];
fullImageView.view.userInteractionEnabled = YES;
fullImageView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
_selectedImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
NSString *key = [self.viewItem.imageKeyArray objectAtIndex:indexPath.row];
_selectedImageView.image = [self.viewItem.itemImages objectForKey:key];
_selectedImageView.clipsToBounds = YES;
_selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
_selectedImageView.translatesAutoresizingMaskIntoConstraints = NO;
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
imageScrollView.delegate = self;
imageScrollView.minimumZoomScale = 1;
imageScrollView.maximumZoomScale = 2;
imageScrollView.translatesAutoresizingMaskIntoConstraints = NO;
[fullImageView.view addSubview:imageScrollView];
[imageScrollView addSubview:_selectedImageView];
viewsDictionary = NSDictionaryOfVariableBindings(imageScrollView, _selectedImageView);
[fullImageView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageScrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[fullImageView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[imageScrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[imageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_selectedImageView]|" options:0 metrics: 0 views:viewsDictionary]];
[imageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_selectedImageView]|" options:0 metrics: 0 views:viewsDictionary]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissFullImageView)];
[fullImageView.view addGestureRecognizer:tap];
[self presentViewController:fullImageView animated:YES completion:nil];
}
I have a UIScrollView and there are many views inside my scroll view. I am using autolayout and all my views are layed out in the same manner: Left and top spacing to the superview, width and height set. Everything scrolls just fine, however my page control stays whereever it is. It does not scroll with the other elements inside the scroll view. YES, I did check that the page control is inside the scroll view just like the other elements, and yes, I've quadruple-checked the constraints of the page control. It just won't scroll. What could be the problem? There are labels, another scroll view, text views, images views and they all scroll perfectly, it's just the page view that is problematic. Is there a bug with Xcode/iOS SDK, or am I missing something?
UPDATE: All the views inside my scroll view are inside a container view. Both the scroll view's and the container view's translatesAutoresizingMaskIntoConstraints property is set to NO. It's only the page control that doesn't obey it's contraints. Here is a screenshot from the Interface Builder:
I got it to work by:
1) Putting all views/controls inside a containerView and declaring it as a property.
2) Adding this code:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.containerView.bounds.size;
}
- (void)viewDidLoad{
[super viewDidLoad];
//added with containerview from g8productions
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(36, 59, 900, 1200)];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:self.containerView];
[self.scrollView setContentSize:self.containerView.bounds.size];
}
Hope either of these solutions work for you!
I made something like that and I update the page control in the scrollview delegate methods:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// If tag == 0 means that is the scrollview for gallery
if (scrollView.tag == 0)
{
self.pageControllGallery.currentPage = self.sv1ScrollViewGallery.contentOffset.x/320;
}
else
{
// Change the bottom label text
int page = (scrollView.contentOffset.x / 320);
[UIView animateWithDuration:0.3 animations:^{
self.labelBottom.alpha = 0.0f;
self.labelBottom.text = [self.arrayOfScrollviews objectAtIndex:page];
self.labelBottom.alpha = 1.0f;
}];
}
}
I calculate the page and set that page in PageControll.
Don't forget to set the Scrollview delegate in .h and in the element.
To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html
If you can dump Autolayout it would be best.
Check out that link, they've got a Pure AutoLayout example at the bottom.
Basically use this code pattern:
#interface ViewController () {
UIScrollView *scrollView;
UIImageView *imageView;
}
#end
#implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
}
I have a search bar on top of a table view set up using auto layout like so:
_searchBar.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[_searchBar]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_searchBar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_searchBar][_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_searchBar, _tableView)]];
Everything looks nice when I run it. But when I do _searchBar.showsScopeBar = YES; before I start editing the search bar, the search bar and table view do not resize automatically. Even when I do [_searchBar sizeToFit], the table view does not resize and move down. Why??
Note: I'm not putting the search bar as the table view's header; it's just a parent view and two subviews.
Note 2: I checked the intrinsicContentSize of _searchBar before and after I call _searchBar.showsScopeBar = YES; and the size does indeed change.
You have to invalidateIntrinsicContentSize:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = YES;
[searchBar invalidateIntrinsicContentSize];
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = NO;
[searchBar invalidateIntrinsicContentSize];
[searchBar setShowsCancelButton:NO animated:YES];
}
See UISearchBar's scope button won't show up iOS6