I want to implement a UIScrollView in my controller because the height of the view is greater than the iphone screen size.
I have my view designed in the story board for which I have created an IBOutlet in .h file
In .m, I am doing the following for creating UIScrollView:
-(void) addScrollView
{
UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.ScrollView addSubview:self.myStoryBoardView];
self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.ScrollView];
}
In the last line of the above, it goes in infinite loop.
Kindly suggest
Replace this line:
UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
With:
self.ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
Explanation:
You are initializing a separate local UIScrollView in your addScrollView method. You local object declaration overrides class instance's object declaration. However following code lines:
[self.ScrollView addSubview:self.myStoryBoardView];
self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.ScrollView];
are called on your class instance UIScrollView's object, which is uninitialized. That's why when you add uninitialized object as subview on any object, your app gets crashed.
You seem to make an UIScrollview (called ScrollView) yet you add self.ScrollView, which is probably a defined IBOutlet property (in your .h or .m file in the interface) made in Storyboard. Try this code and get rid of the UIScrollView in your Storyboard.
Also don't use ScrollView but instead use scrollView as apple documentation suggests.
-(void) addScrollView
{
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:self.myStoryBoardView];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scrollView];
}
He is right. Either add scroll view in the storyboard or from the code. You are setting height of scroll view equal to the height of your self.view instead set height to self.myStoryBoardView.
-(void) addScrollView
{
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:self.myStoryBoardView];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.myStoryBoardView.frame.size.height);
[self.view addSubview:scrollView];
}
Related
The problem shows in the gif picture. (The view in yellow is the contentview. The view in black is the scrollview.)
The whole project code on Github.
Here is the code :
CGRect frame = CGRectMake( 50, 100, 200, 200);
UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:scrollView];
frame= CGRectMake( 0, 0, 500, 500);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500,500);
scrollView.backgroundColor = [UIColor blackColor];
myImageView.backgroundColor = [UIColor yellowColor];
scrollView.contentOffset = CGPointMake(0, 0)
One more problem shows in the picture:
Why the horizontal scrollbar is not at bottom of scrollview?
When I added a view between the scrollview and self.view the problem disappeared.
The code that I added:
UIView *view = [UIView new];
[self.view addSubview:view];
I found that the problem happened when I used UITabBarController.
Make the UIScrollView a global variable. Make the contentInset of the UIScrollView and UIScrollView's indicators UIEdgeInsetsZero when the views are loaded like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
I am a newbie to IOS app development. I am trying to implement a scrollview. I am not able to scroll at all.
Below is my view implementation.
I created a scroll view and added small blocks with different colors in it.
I added one block out of current screen bounds so that I can scroll down the UIScrollView and view it.
#implementation CustomView
- (instancetype)init
{
self = [super init];
if (self) {
CGRect screenRect = CGRectMake(70, 100, 250, 800);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
scrollView.scrollEnabled = YES;
[scrollView setBackgroundColor:[UIColor yellowColor]];
[scrollView setAlpha:0.2];
[self addSubview:scrollView];
CGRect temp = CGRectMake(100, 150, 10, 10);
UIView *firstView = [[UIView alloc] initWithFrame:temp];
[firstView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:firstView];
temp = CGRectMake(150, 200, 10, 10);
UIView *secondView = [[UIView alloc] initWithFrame:temp];
[secondView setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:secondView];
temp = CGRectMake(200, 750, 10, 10);
UIView *thirdView = [[UIView alloc] initWithFrame:temp];
[thirdView setBackgroundColor:[UIColor orangeColor]];
[scrollView addSubview:thirdView];
scrollView.contentSize = screenRect.size;
}
return self;
}
#end
I am using the view I created before in my view controller. I am adding it to my view controller's view.
#interface ViewController ()
#property (nonatomic, readonly) CustomView *currentView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_currentView = [[CustomView alloc] init];
[self.view addSubview:_currentView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am not able to scroll at all with this implementation. am i missing something ?
Can someone please explain what is wrong with my code.
Thank you
When you add a scrollview to the screen it should be the size which you want to display on the screen. It will be displayed on the screen simply as an UIView only when you look at it.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(20, 20, 250, 250)];
The above initialisation will give you a scroll view of the size 250X250 placed at the position (20,20).
For scrolling you need to define the content size of the scroll view.
This content size should be bigger than your scrollview size otherwise you will not be able to differentiate it. The size of the contentsize should be decided based on the content you are going to put inside it.
For example if the three views you are adding inside the scrollview occupy a total width of 500 and a height of 300 then your content size should be equal to that only.
[scrollView setContentSize:CGSizeMake(500, 300)];
This will allow you to scroll inside the scrollview.
Also the content you are adding to you scrollview should be added considering the top left corner of the scrollView as the (0,0) location.
So if you want to display something on the top left corner of your scrollView then the frame dimensions of that particular view should be like this
CGRectMake(0,0,30,30);
This will add a view of the size 30X30 on the top left corner of the scrollView.
Hope it helps.
Your scroll view doesn't scroll because its frame is as large as its content size. Add this line in the view controller after before adding the scrollview as subview:
_currentView.frame = self.view.bounds ;
The contentSize property should not be the size of the screen, it needs to be larger if you want scrolling to work. You should calculate the size of the content within the scrollView, for example the combined height of all the subviews and the spaces between them or maybe the MaxY of the bottom view.
Also, try using a UITableView (or a UIStackView in iOS 9) to accomplish the same behavior without doing any math
First of all, what you are trying to do should be done using UITableView. I am not sure why are you trying to do it this way.
Anyhow, did you try to set also the contentSize property of UIScrollView?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/contentSize
I use the code snippet below to create my UIScrollView:
scrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
scrollView.contentSize = CGSizeMake(320, 737);
[self.view addSubview:scrollView];
[_scrollView addSubview:myView];
CustomScrollView is subclass of UIScrollView.
myView is a UIVIew from .xib and there is a UIButton in myView.
The issue is: When the Y position of the UIButton is smaller than 568, it can receive touch events, but when it's larger than 568, for example, 680, it fails to response any touch event.
SDK is iOS8.1.
How can I solve this problem?
My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.
I have a container view inside the scroll view that has a width of 960 points.
At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.
This is my viewDidLoad method:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView.scrollEnabled = YES;
[self.scrollView setFrame:CGRectMake(0,0,320,300)];
self.scrollView.contentSize = CGSizeMake(960, 300);
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
[containerView addSubview:subview1];
[self.scrollView addSubview:containerView];
}
Here is a sample code for create a scroll view programmatically:
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];
[scrollView addSubview:containerView];
So, there is no issue in your code, but something wrong with your storyboard configure.
I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.
Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?
If you can give me more information about this issue, I can help you.
Within my UIView, I have a UIScrollView which fills the first view, so than when the content is bigger than the iPhone screen size, the user can scroll the page down. It works well, but when the user finishes the scroll movement - i.e. removes his fingers, the page snaps back into it's original position. Obviously that is not what I want, how can it be avoided?
Here is the relevant code in the UIView class which declares and uses the UIScrollView class.
#implementation TestView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.canCancelContentTouches=NO;
[self addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 500);
[scrollView setContentSize:scrollViewContentSize];
CGRect rectForBigRedSquare = CGRectMake(50, 50, 200, 200);
UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
[redSquare setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:redSquare];
return self;
}
An additional question is this: how is it possible to make it such that the user can only scroll down, that is to see content at the bottom which was out of view, but not to scroll up so that there is space before the start of the content. In
Basically you just have to set contentSize of your scrollview according to the contents.
CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];
Okay, the easiest way to get this scrollview working as you desire is to ensure that content size of the scrollview is identical to the frame size of the content you wish to scroll.
You can achieve this by having a content view into which you add all the views you wish to be visible and then add that content view to the scrollview while ensuring that the content size of the scrollview is set to the content view's frame size.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1280, 460)];
UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[redView setBackgroundColor:[UIColor redColor]];
[contentView addSubview:redView];
[redView release];
UIView* blueView = [[UIView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
[redView setBackgroundColor:[UIColor blueColor]];
[contentView addSubview:blueView];
[blueView release];
CGSize contentViewSize = contentView.frame.size;
[scrollView addSubview:contentView];
[scrollView setContentSize:contentViewSize];
[contentView release];
[self addSubview:scrollView];
The app I was working on had similar symptoms. The user could scroll down but on release the view would snap back to the initial position. The page was set up as follow:
[VIEW]
[SAFE AREA]
[SCROLL VIEW]
[CONTENT VIEW]
I strongly suspect that a combination of Auto-Layout and manual constraints caused by several adjustment iterations was causing the issue. To resolve this all constraints where removed from the View.
The Scroll View was assigned the following constraints:
Scroll View.leading = Safe Area.leading
Scroll View.top = Safe Area.top
Scroll View.trailing= Safe Area.trailing
Scroll View.bottom = Safe Area.bottom
The Content View was then assign the following constraints
ContentView.Leading = Scroll View.Leading
ContentView.top = Scroll View.top
ContentView.centerX = ScrollView.centerX
The Content View was also given the following self constraint
height = 1000