UIScrollView doesn’t seem to work in xcode 6 (iOS 8) - ios

I’m trying to make a scroll view in xcode. I used the following code in xcode 5 (which worked):
view.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *ScrollView;
}
#end
view.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[ScrollView setScrollEnabled:YES];
[ScrollView setContentSize:CGSizeMake(320, 1005)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have seen others with this problem but none of their solutions worked for me. Note that i have size set to freedom and auto layout is turned off.

Try this - UIScrollView with Autolayout
OR
try setting your content size in viewDidAppear

Due to a scrollView feature in auto layout, the contentSize is being redefined. To solve this problem, you could disable auto layout or set the content size in viewDidLayoutSubviews()
-(void)viewWillLayoutSubviews {
[super viewDidLayoutSubviews];
ScrollView.contentSize = CGSizeMake(320,1005);
}
So you would want to add this code and delete the ScrollView.contentSize method in viewDidLoad.

I don't see anything immediately wrong with this code. I would check and makes sure that you've connected the IBOutlet properly in the Interface Builder.

Related

Display nib above of my storyboard view

I'm trying to essentially load a nib into view when a certain criteria is met. I'm able to show the nib on my storyboard thought I cant seem to be able to execute any actions when the button is pressed.
Just to express my ultimate goal. I would like to create sort of a template with my nib view and then display the nib view from my storyboards but manipulate the labels in my nib view with values sent from the storyboard.
OK so I'll do my best to try and show my detail steps on how i try to accomplish this.
Firstly here are is a picture of my project.
my VCDemoView.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
VCDemoView.m
#import "VCDemoView.h"
#interface VCDemoView ()
#property(nonatomic) IBOutlet UIImageView *imageView;
#property(nonatomic) IBOutlet UILabel *titleLabel;
#property(nonatomic) IBOutlet UILabel *subtitleLabel;
#property(nonatomic) IBOutlet UIView *container;
//- (IBAction)changeLabel:(id)sender;
#end
#implementation VCDemoView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(void)initalizeSubviews
{
//Load the contents of the nib
NSString *nibName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[nib instantiateWithOwner:self options:nil];
//Add the view loaded from the nib into self.
[self addSubview:self.container];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)changeLabel:(id)sender {
//[self.subtitleLabel.text = #"MIGUEL"];
_subtitleLabel.text = #"miguel";
}
#end
storyboard view controller.
#import "ViewController.h"
#import "VCDemoView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
[self.view addSubview:CustomView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Can anyone spot what I'm doing wrong? I cant seem to be able to communicate with the button? I'm sure is something silly.
here is a link to the dropbox project with all the source code if you prefer to see it that way.
https://www.dropbox.com/sh/23cbhs4qvsxwei0/AADFs6MN3eqJNK42Io2etuJea?dl=0
Thanks is advance guys.
You never set the frame of your VCDemoView. The frame defaults to CGRectZero.
A view doesn't clip its subviews by default, so you can still see the labels and the button, even though they're outside the bounds of the VCDemoView. But a view does not receive events outside its bounds, so when you tap on the button, the top-level view swallows the event because it finds that the event is outside the bounds of its one subview (the VCDemoView).
You can fix the frame of your VCDemoView like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
CustomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CustomView.frame = self.view.bounds;
Note that you can use autoresizing even though your storyboard uses autolayout constraints. Autolayout will turn the autoresizing mask into constraints for you.
Or you could just put your VCDemoView in your storyboard as a child of the root view and set up the constraints there.
You should not call and action inside a View this what the Controller should handle .
You need to change the fileOwner to be the controller and the View Class to be VCDemoView , connect the View to the controller in the IB , and load the nib from there , then add it as a subView to the ViewController's view .
this the edited code ,, I hope it helps .. https://dl.dropboxusercontent.com/u/33359624/demo-SO/Archive.zip
You are not set the CustomView frame so the default frame is CGRectZero. if you set the frame of the CustomView it will works fine.
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;
CustomView.frame =self.view.frame;
[self.view addSubview:CustomView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Image Not Showing Up on UIScrollView

So I have a UIScrollView and scrolling works. I added a label on it and it scrolled that too. When I added an Image View, however, scrolling works but the image does not show up and I get this error:
Could not load the "block" image referenced from a nib in the bundle with identifier
My ViewController.m:
#interface ViewController : UIViewController{
IBOutlet UIScrollView * scroller;
}
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 600)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I have disabled "Use Auto Layout," Changed the Size to "Freeform," and changed the views height to 600.
Any ideas on how I can make this work?

Why can't I access UIImageview.center?

I'm working on an old project of mine with a xib file in it. I have a button that changes the position of an UIImageview to 0,0, but it doesn't work and the label stays at its initial position. When I create new project with the exact same code expect it contains a mainStoryboard.Storyboard it works fine. I suppose it has something to do with the xib file and files owner.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Action1:(id)sender {
imageview.center = CGPointMake(0, 0);
}
If you're using auto layout, changing the frame may not have any effect.
Instead, you need to change the constraints on the view.
You can find a written tutorial on auto layout on Ray Wenderlich's site here or a video tutorial here.

Can't get a UIScrollView to zoom and scroll

I know this has been posted about a million times, but I cannot for the life of me get this Scrollview to work how it should. I have followed about four different tutorials and have spent the last two days attempting to fix this, so I really do hope someone can help me through this (if there happens to be another post that fixes my problem that I didn't see, I'm really sorry).
I have an Imageview inside of a Scrollview, containing a picture I'd like to be zoomable/scrollable when it's zoomed.
In my WDViewController.h file:
#import <UIKit/UIKit.h>
#interface WDViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImageView *img_wd;
#end
And in my WDViewController.m file:
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Autolayout is disabled, as I noticed that caused problems to other users on Stack Overflow. I made sure that User Interaction and Multiple Touch were enabled in the Storyboard. But when I launch the app in a simulator, it just sits there. No matter how many times I pinch it doesn't zoom. I thought I did everything, but I must have missed a step that I'm just not seeing for some reason. Can anyone give me a hand?
EDIT: After the comments below, here's the code I have now (for WDViewController.m, .h hasn't changed):
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:self.img_wd];
self.img_wd.frame = CGRectMake(0, 0, 1280, 960);
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDIT: And after testing it on my phone, it works. Um... okay then, thanks everyone. My simulator just refuses to accept it. How odd.
Just a hunch:
To pinch in the iOS Simulator, you have to hold down the option key.
View hierarchy in IB:
The scroll view was dragged and dropped onto the root view and was snapped into place to fill the entire root view. Image view was dragged and dropped onto the scroll view and was snapped into place to fill the entire scroll view.
Attributes for image view in IB:
All source code:
did you add the image view to scrollview's subview? [self.scrollView addSubView:self.img_wd]

iOS UIScrollView bounces back to the top

When i scroll using UIScrollView downwards and let go it won't stay where i scrolled down to it bounces back to the top
http://www.joules.name/ScreenShot2013-01-25at19.11.45.png
http://joules.name/ScreenShot2013-01-25at19.04.12.png
in ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIScrollView *scoller;
}
#end
and in ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidAppear
{
[scoller setScrollEnabled:YES];
[scoller setContentSize:CGSizeMake(320, 1000)];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Its probably because you set the actual height of the scrollview to 800 in the nib file. Change that to fit the screen.

Resources