View not scrolling when simulator set to landscape mode iOS - iphone

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

Related

Objective - C UIScrollView not working horizontally

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)
}

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 :)

Scroll Views in storyboarding iphone

I'm trying to set up a scrollView in an app using storyboarding. I have done everything correctly but the scrollview won't scroll in simulator or on iPhone.
MoreViewController.h
#property(nonatomic, retain) IBOutlet UIScrollView *moreScroll;
MoreViewController.m
-(void)viewDidLoad{
[moreScroll setScrollEnabled:YES];
[moreScroll setContentSize:CGSizeMake(1220, 354)];
}
I have connected the scrollView to the files owner, can someone help please
Thanks in Advance
With autolayout is there a new method - (void)viewDidLayoutSubviews
Here is a quick info:
Notifies the view controller that its view just laid out its subviews.
When a view’s bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
You should be fine if you add this:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubViews];
[moreScroll setContentSize:CGSizeMake(1220, 354)];
}
Setting an scroll view’s scroller style sets the style of both the horizontal and vertical scrollers. If the scroll view subsequently creates or is assigned a new horizontal or vertical scroller, they will be assigned the same scroller style that was assigned to the scroll view..
You should be if you add this lines in your code:
This line in .h file
#interface Shout_RouteView : UIViewController<UIScrollViewDelegate>
{
}
#property (retain, nonatomic) IBOutlet UIScrollView *scrollViewMain;
This lines copy in .m file
-(void)viewWillAppear:(BOOL)animated {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
scrollViewMain.frame = CGRectMake(0, 0, 768, 815);
[scrollViewMain setContentSize:CGSizeMake(768, 1040)];
}else {
scrollViewMain.frame = CGRectMake(0, 0, 320, 370);
[scrollViewMain setContentSize:CGSizeMake(320, 510)];
}
}
I suggest that you try to pust the content setting code into the viewWillAppear or viewDidAppear method. Its always best not attempt changing UI features before it is ready for display.

Loading a view into a UIScrollView AND getting the content inside to work

I have a tab based application I am working on.
I have a view controller named DetailsView.m, with an accompanying nib file called DetailsView.xib. This has a couple of UILabels in, which are linked using IBOutlet to DetailsView.m view controller. When I load this view controller/view using the tab bar controller, it works fine and the UILabels are populated dynamically.
Now I want to load this entire view inside a UIScrollView instead so I can fit more content in.
So I created another view controller called DetailsHolder.m with a nib file called DetailsHolder.xib, and assigned this to the tab bar controller.
I wrote this code below to load the first view (DetailsView) into the UIScrollView in the second view (DetailsHolder). I wrote it in the viewDidLoad method of DetailsHolder:
DetailsView* detailsView = [[DetailsView alloc] initWithNibName:#"DetailsView" bundle: nil];
CGRect rect = detailsView.view.frame;
CGSize size = rect.size;
[scrollView addSubview: detailsView.view];
scrollView.contentSize = CGSizeMake(320, size.height);
This correctly loads the sub view into the UIScrollView, however, the labels inside DetailsView no longer do anything. When I put an NSLog inside viewDidLoad of DetailsView - it never logs anything. It's as if I've loaded the nib ok, but its no longer associated with the view controller anymore. What am I missing here? I'm a bit of a newbie in obj C/iOS (but have many years Actionscript/Javascript knowledge.
Thanks in advance,
Rich
Edit: Contents of DetailsView as requested:
DetailsView.h:
#import <UIKit/UIKit.h>
#import "AppModel.h"
#interface DetailsView : UIViewController {
IBOutlet UITextView* textView;
IBOutlet UIImageView* imageView;
}
#end
DetailsView.m
#import "DetailsView.h"
#implementation DetailsView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
AppModel* model = [AppModel sharedInstance];
[model loadData];
int selectedLocation = [model getSelectedLocation];
NSArray *locations = [model getLocations];
NSArray *data = [locations objectAtIndex:selectedLocation];
textView.text = [data objectAtIndex: 0];
UIImage *theImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[data objectAtIndex: 4] ofType:#"jpg"]];
imageView.image = theImage;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Essentially all its doing is grabbing selectedLocation (int) from a singleton (which is my model), then grabbing an array from the model, and then trying to insert an image and some text into my view. In my nib file, i have a UIImageView and a UITextView, which I have linked to the two IBOutlets declared in DetailsView.h
When you use the view controller like this, many events will not occur in the view controller, e.g. viewWillAppear, viewWillDisappear and device rotation event. My best guess that you did some initialisation in some of those event methods. Posting your code for DetailsView would make it easier to find the problem.

MKMapview freezes when zoomEnabled=NO and call setRegion (iPhone SDK)

I am a beginner in iPhone development and I have come across a problem with MKMapView. First let my describe how to reproduce the problem.
Create a view-based application
In Interface Builder, add a MapView that cover the whole view area
Add a UIButton over the Mapview (size 50x50 is fine, put it in a corner)
Associate the MapView in IB with a MapView reference in the code
Associate the UIButton to an handling method (touch down).
Here's the code for the controller (MapTest3ViewController.h)
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapTest3ViewController : UIViewController {
MKMapView *myMapView;
}
#property (nonatomic,retain) IBOutlet MKMapView *myMapView;
- (IBAction) zoomClicked:(id)sender;
#end
(MapTest3ViewController.m)
#import "MapTest3ViewController.h"
#implementation MapTest3ViewController
#synthesize myMapView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//myMapView.zoomEnabled = NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (IBAction) zoomClicked:(id)sender
{
// when zoom button is clicked, zoom on a specific region
CLLocationCoordinate2D locationCoordinate2D;
locationCoordinate2D.latitude = 35.6994;
locationCoordinate2D.longitude = 139.78;
MKCoordinateSpan coordinateSpan;
coordinateSpan.latitudeDelta = 1;
coordinateSpan.longitudeDelta = 1;
MKCoordinateRegion coordinateRegion;
coordinateRegion.center = locationCoordinate2D;
coordinateRegion.span = coordinateSpan;
[myMapView setRegion:coordinateRegion animated:YES];
}
#end
I have set scrollEnabled=YES, zoomEnabled=YES and showUserLocation=YES in IB. The application WORKS fine. I can zoom using pinching, I can scroll, whenever I push the "Zoom" button the view area moves to the region defined in zoomClicked.
The problem
When I set zoomEnabled=NO in viewDidLoad, I can no longer zoom using pinching gestures which is fine. However, if I "play" with the map, scrolling, trying to zoom(which of course doesn't work), then push the zoom button the map freezes and I can no longer scroll or do anything.
Again, how to replicate the problem (zoomEnabled=NO)
After application loads, the map of the entire earth appears. Do some pinching gestures in and out, scroll the map. (Zoom doesn't work which is expected)
Push the zoom button which should set the map the a specific region (in the code Tokyo, Japan) -> the map freezes.
My setup
iPhone OS 4.0.1/iPhone 3GS/XCode 3.2.3 64bit/Testing done on the phone itself.
Any help would be greatly appreciated.
Thanks.