Objective - C UIScrollView not working horizontally - iphone

i read all the other replies for similar question but my project still don't work.
Any help?
This is my project.
----------------> viewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIScrollView *scroll;
IBOutlet UIImageView *imagev;
}
----------------> viewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imagev.image = [UIImage imageNamed:#"ka.jpg"];
[scroll setContentSize:CGSizeMake(700, 700)];
[scroll setScrollEnabled:TRUE];
[scroll setShowsVerticalScrollIndicator:YES];
[scroll setShowsHorizontalScrollIndicator:YES];
}
#end
everything looks fine but the scrollview won't scroll..
EDIT: as they suggested here, the problem was Autolayout. with that i fixed the scrollview

I've got a fix for you, and you can continue to use autolayout.
Implement the following method in "ViewController.m"
- (void)viewDidLayoutSubviews
{
[scroll setContentSize:CGSizeMake(700, 700)];
}
And you can remove [scroll setContentSize:CGSizeMake(700, 700)]; from the -viewDidLoad method.
This will fix it for you, and you can continue to use auto-layout. Auto-layout is a bit dodgy in iOS 6. But it is apparently much better in iOS 7.

I believe Autolayout is causing the problem. Try turning it off.
Select the ViewController in the Storyboard or NIB
Click the 'File Inspector' tab on the right hand side of your Xcode window
Under the 'Interface Builder Document' section, uncheck 'Use Autolayout'

Objective - C
you can use this lines of code into your *.m file's - (void)viewDidLoad{} function:
[scroll setContentSize:CGSizeMake(320, 800)];
[scroll setScrollEnabled:TRUE];
[scroll setShowsVerticalScrollIndicator:NO];
[scroll setShowsHorizontalScrollIndicator:YES];
for this you need to take an IBOutlet property of UIScrollView into your *.h file this way:
IBOutlet UIScrollView *scroll;
And connect this from Storyboard.
Or,
You can use this method into your *.m file:
-(void)viewDidLayoutSubviews {
[scroll setContentSize:CGSizeMake(320, self.view.frame.size.height* 1.5)];
// this will pick height automatically from device's height and multiply it with 1.5
}
This both solution works for me in xcode-5, xcode-6, xcode-6.1, xcode-6.2
Swift:
You can use this swift code for similar type of issue:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSize(width:10, height:10)
}

Related

View not scrolling when simulator set to landscape mode iOS

I went through many blogs, stack flow questions and googled alot, but didn't get any solution to get my ScrollView work. I don't know why but my ScrollView works in portrait mode but not in landscape.
I am developing using xcode 5 for ios 7 devices. I developed my screen using storyboard. I added a ViewController on storyboard, removed the default UIView it contains, and added a ScrollView. In that ScrollView I added my subviews. Then I referenced the ScrollView to the scrollview object in the outlets.
This is my LoginViewController.h
#import <UIKit/UIKit.h>
#interface LoginViewController : UIViewController
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#end
This is my LoginViewController.m
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
#synthesize scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
My scrollview scrolls when in portrait mode but not in landscape. Please help me. Thanks in advance.
Reset scroll view content size on screen orientation:
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
Scroll view frame most probably will change so you need to re-set content size.
if you have auto layout on set content size inside layoutSubviews:
- (void) layoutSubviews
{
[super layoutSubviews];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}
Note
Content size supposed to be larger than scroll view size. to scroll right?
You want to add scrolling in scroll view check scrollView height.
If scrollView.height > scrollview.content.height scrollview did not scroll. But scrollView.height < scrollview.content.height scrollview will scroll....
So, the scrollview's content height is greater than the scrollview height is necessary for scrolling..
Therefore, you should do something like this:
scrollView.contentSize.height=scrollView.height+50

Why won't UIScrollView scroll fully after adding objects? Using storyboard, ARC, and Xcode 4.5.2

So, I know there are similar questions to mine, but maybe not exact (so please don't mark me down -- just warn me or something). I have searched for days for the solution to this SIMPLE issue. Using storyboards, ARC, and Xcode 4.5.2, I simply need to put a bunch of labels inside a UIScrollView and have it scroll vertically. I've tried so many combinations of setting frame sizes and content sizes within viewDidLoad, viewDidAppear, and viewWillAppear, but to no avail. The scroll view scrolls perfectly when there's nothing inside of it, but when I add labels to it, the scrolling only scrolls a very short section.
Note: I need to use auto layout, otherwise my whole project will get messed up.
Here's my current code...
.h file:
#import <UIKit/UIKit.h>
#interface MortgageRatesViewController : UIViewController <UIScrollViewDelegate, UIScrollViewAccessibilityDelegate>
- (IBAction)backButton:(id)sender;
#property (strong, nonatomic) IBOutlet UIView *mortgageView;
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
.m file:
#import "MortgageRatesViewController.h"
#interface MortgageRatesViewController ()
#end
#implementation MortgageRatesViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//-------------------------------------------------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"appBackgroundColor.png"]];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(0, 809)];
}
//---------------------------------------------------------------
//---------------------------------------------------------------
//-(void)viewWillAppear:(BOOL)animated{
//
//
// [super viewWillAppear:animated];
//
//
// [self.scrollView setFrame:CGRectMake(0, 0, 320, 808)];
//
//
//}
//---------------------------------------------------------------
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view addSubview:self.scrollView];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(0, 809)];
}
//-------------------------------------------------------------
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:NO];
}
#end
Note: having viewWillAppear commented out has made no difference.
EDIT: I POSTED THE SOLUTION BELOW. HOPE IT HELPS OTHERS!
After days of research, it's funny I actually found the solution just minutes after posting this question! So, for my situation I simply had to add this bit of code, and it worked:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.scrollView setContentSize:CGSizeMake(320, 808)];
}
It seems to be working perfectly now. Please, anyone, let me know if this is a poor way of doing it, because I'm new to this and I would love any help. Otherwise, I'll leave it and hope this can help other people! Thanks!
SOLUTION with Storyboard + Autolayout (no code):
Vertical scrolling example that starts from top-left corner:
Set the top+leading constraints of the first object (subview)
Chain up every object till the last one
The most important - set the bottom constraint to the scroll view so it's hooked up top to bottom.
Set the width of the scroll view - either a specific "magic number" or a better way - connect the trailing constraint of a subview that has a set width - if there's non I usually set a label's width = screen width - side paddings => standard(20)+UILabel(280)+standard(20) - this way the scroll view knows exactly its content's width, otherwise you'll get an "ambiguous width" warning.
*Needs constraints' adjustments if horizontal scrolling is desired.
your scrollview's frame is not the scrollable size.
Check out contentSize
Set frame to its superview.bounds then set content size to the scroolview's scrollable area.
Also I dont know that the background will scroll.
You will want to add subview's to the scrollview itself.
It should scroll its own subviews. but it will not scroll itself :)

how to load uiview from nib

I am trying to change views when I swipe. I have the swipe working find as I have tested this by changing the background color.
Since then however I have added a new view.nib and set the class to be the same as the current view.
Inside this classes .h file I have done this
#interface MasterViewController : UIViewController <UIGestureRecognizerDelegate>{
UIView *myView;
UIView *secondView;
}
#property (strong, nonatomic) IBOutlet UIView *myView; // attached to Viewcontroller nib
#property (strong, nonatomic) IBOutlet UIView *secondView; // attached to the secondView
- (void)swipedScreen;
MyView is the main view that appears first, secondView is the view of the nib that I have created and changed its class to relate to this view.
From there I have done this in the .m file
- (void) setupSwipeGestureRecognizer {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft);
[myView addGestureRecognizer:swipeGesture];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Prototype";
[self setupSwipeGestureRecognizer];
}
- (void)swipedScreen
{
// Not sure what to do here.
[[NSBundle mainBundle] loadNibNamed:#"SecondView" owner:self options:nil];
}
I just dont know what to do in swipedScreen method to get the secondView to appear.. I would like to animate this view to slid in from the right.. but at the moment that comes secondary to actually just getting the view to appear... not sure what I am doing wrong here but obviously its something quite fundamental.
any help would be greatly appreciated.
As I commented:
- (void)swipedScreen
{
[UIView animateWithDuration:3.0f delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
secondView.frame = CGRectMake(180, secondView.frame.origin.y, secondView.frame.size.width, secondView.frame.size.height);
}
completion:^(BOOL finished){
myView.hidden = YES;
}];
}
Actually, you'll have to change the X, Y, Width and height values as you want to animate, and when it's completed, I set the main view - myView - hidden.

iPhone SDK - Adding a static image over a UITableViewController

I am trying to add a static fixed image to a UITableViewController, but when I do the standard [self.view addSubview:imageView]; the image is placed on the tableview and moves with the scrolling.
Is there any way to do this so that the image stays fixed?
I know one method would be to create a UIViewController, then add the UIImageView and a UITableView, but unfortunately, I am using a custom UITableViewController (just a library found on gihub to do what I needed), so my controller must be a UITableViewController.
Is there any way to do this? I've been going at this for a while with no luck.
Cheers,
Brett
There is no problem using UIViewController idea. You just keep 2 view controllers: 1) UIViewController, which has the UIImageView inside, and subview the view of 2) the UITableViewController. If necessary, make the UITableViewController a strong reference of the UIViewController.
I have done something similar all the time.
Yes, there are few ways. You could create your view hierarchy programmatically at
viewDidLoad or use a NIB file. Make sure that you correctly link the delegates and view properties.
If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.
If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.
As https://stackoverflow.com/a/6961973/127493 say, UITableViewControllers can be replaced by simple UIViewControllers.
In fact, the trick is to add an UITableView to you UIViewController, make it delegate and etc..., and add it to your UIViewController.view.
So you will be able to add some "sister" views to your controller main view.
In my case, I am adding a an Image ( actually button with image) and when user touches on image, it will disappear and tableview will be shown.
so i am disabling scroll first then enable it back
find code below
// in viewDidLoad
[self.view addSubview:imgview];
tbl.scrollEnabled = NO;
// in -(IBAction)btnClicked:(id)sender
[imgview removeFromSuperview];
tbl.scrollEnabled = YES;
Thats working for me.
Do NOT use UITableViewController at all (I never use it and as I've heard nearly any developer uses it). It is a nightmare when you want to customize design with it.
Create your own subclass of UIViewController (MYTableViewController), add UITableView *tableView instance #property and #synthetize it:
#interface MYTableViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
UITableView *tableView;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#end
Then in implementation add it to the view (using XIB or viewDidLoad method):
#implementation MYTableViewController
#synthesize tableView;
// If not XIB used:
-(void)viewDidLoad{
[super viewDidLoad];
CGRect frame = self.view.bounds;
self.tableView = [[[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
// And here you van add your image:
[self.view addSubview:imageView];
}
// Do not forget to release it and clear delegate and datasourcce when view uloads:
#pragma mark - Memory management:
-(void)dealloc{
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
self.tableView = nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning {
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
self.tableView = nil;
[super didReceiveMemoryWarning];
}
-(void)viewDidUnload{
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
self.tableView = nil;
[super viewDidUnload];
}
#end

scrollViewWillBeginDragging not responding on a second UIScrollView

I have a probably nub question but i just can't get around this.
I have a UIScrollview on the top of the screen, below it there is a UITableView.
both are on my InterfaceBuilder, not done programmatically
and declared on my .h as:
#interface RootViewController : UIViewController <UIScrollViewDelegate, IconDownloaderDelegate>
{
IBOutlet UIScrollView *scrollview;
}
etc.
Inside the UIScrollView i have a long tabbar also in IB mi code is as follows for this:
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(1100, 29)];
Everithing works perfectly the tabbar works and moves etc.
the problem is that when implementing scrollViewWillBeginDragging
it only registers the scrolling on my UITableView and not on the scrollview.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(#"done");
}
What am i doing wrong??
Thanks in advance!
You have to set the delegate to the class where you implement the delegate methods.
scrollview.delegate = self;
add [scrollview SetDelegate:self]; in the place you add scrollview