UIScrollView does not scroll - ios

I got a problem about UIScrollView. I am making a custom view which inherits UIView. The view has a UIScrollView on which there are lots of buttons which should scroll left and right. The UIScrollView and buttons can show normally. But I cannot scroll the buttons. Could someone give me some suggestions? Thanks a lot!
MZMPhotoCalenderSwitcher.h
#import <UIKit/UIKit.h>
#interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *topSwitcher;
#end
MZMPhotoCalenderSwitcher.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
self.topSwitcher.backgroundColor = [UIColor greenColor];
self.topSwitcher.pagingEnabled = YES;
self.topSwitcher.showsHorizontalScrollIndicator = NO;
self.topSwitcher.showsVerticalScrollIndicator = NO;
[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
- (void)add:(int)num ButtonsOnView:(UIScrollView *)view withButtonWidth:(CGFloat)width andHeight:(CGFloat)height
{
CGFloat totalTopSwitcherWidth = num * width;
[view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];
CGFloat xOffset = 0.0f;
for (int i=1; i<=num; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(xOffset, 0, width, height)];
xOffset += width;
[button setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[button setTag:i];
[button addTarget:self action:#selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
if (i % 2 == 0)
[button setBackgroundColor:[UIColor yellowColor]];
else
[button setBackgroundColor:[UIColor redColor]];
[view addSubview:button];
}
}

Below following line
[view addSubview:button];
add
view.contentSize = CGSizeMake(view.contentSize.width, button.frame.origin.y + button.frame.size.height);
This will set content size of scroll view to the bottom of your button.

It should be issue of content size, also check that scrollEnabled property of UIScrollView object is set to TRUE,
Check solution of UIScrollView won't scroll!

Just disable "Auto Layout" in your project and it will start working!

I'm using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:
The scroll view content size height MUST be bigger than the screen
height, this almost goes without saying...
The top/bottom/left/right constraint of the scroll view HAS TO BE
pinned, I did this using storyboard so no code showing up here
If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.
This worked for me, hope it can help someone.

Combining all of this advise into one simple answer:
// In viewDidLoad
// set the size of the scrollview (in this case I made it the size of its container
self.scrollView.frame = self.view.frame;
// now set the size of the content to be scrolled within the scrollview
// the content to be displayed must be bigger than the scrollView
// in this case I added 100 to make the content size, so that it is taller than the height of the scrollView
// now it scrolls
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 100);

Your problem is that you haven't set your contentSize, therefore, the contentSize is the same than the frame view.
You need to do this in your viewDidLoad
- (void)viewDidLoad {
........
self.scrollView.contentSize = CGSizeMake(/*yourWidth*/, /*yourHeight/*);
........
}
Try do adjust your contentSize to the last position of your object plus its size plus a padding.

You have to set scrollview frame size and content size.

Where are you add ScrollView on SuperVIew? And where are you recalculate Content size property of scrollView?
[view setContentSize:CGSizeMake(<summYourViews.width>,<summYourViews.height>)];

try this . . . .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
self.topSwitcher.backgroundColor = [UIColor greenColor];
self.topSwitcher.pagingEnabled = YES;
self.topSwitcher.showsHorizontalScrollIndicator = NO;
self.topSwitcher.showsVerticalScrollIndicator = NO;
[topSwitcher setContentSize:CGPointMake(self.view.bounds.size.width,TOP_SWITCHER_HEIGHT+400)];
[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}

It's amazing.
I just move the codes which was in viewDidLoad to initWithFrame:, then it works. I do not know why it is?

Related

UIScrollView scroll functionality not working?

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

View with subviews in a uinavigationbar and uitoolbar

I really need your help with this one (first post on SO -- be gentle):
I have two dynamic UIButtons which I would like to have centered in a UIView, which in turn should be centered in a UINavigationbar and UIToolbar. I can't - despite a lot of Googling - seem to figure out a proper way to do this.
This is what I've done so far:
In viewDidLoad, I add the two buttons as subviews and set the view as the UINavigationbar's titleView
self.myClass.viewForTitleAndButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 32)];
[self.myClass.viewForTitleAndButton setBackgroundColor:[UIColor blackColor]];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myButton];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myOtherButton];
self.navigationItem.titleView = self.myClass.viewForTitleAndButton;
In a method being triggered when I press certain buttons, I set the title (and bounds) of one of the buttons depending on what's clicked:
CGSize titleSize = [title sizeWithAttributes:#{NSFontAttributeName : [UIFont italicSystemFontOfSize:17.0]}];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat newX = (screenSize.width - titleSize.width) / 2;
CGRect buttonFrame = self.myClass.myButton.frame;
//Removing the line below doesn't do any difference at the moment
self.myClass.myButton.bounds = CGRectMake(newX, buttonFrame.origin.y, titleSize.width+8, buttonFrame.size.height);
[self.myClass.myButton setTitle:title forState:UIControlStateNormal];
NSLog(#"Title: %#", title);
//title is a NSString that changes depending on what is clicked. I am 100% sure it changes as I can see it in the log every time the method is triggered.
The problem is that the title of myButton is not changed. It worked before with the very same button when it was placed in a different spot and not as a subview.
Q1: What am I missing to make the title of the button change?
Q2: Is adding the buttons as subViews to a view that is then placed in the navigationbar and toolbar respectively a sound way to accomplish what I want?
This is really bugging me, any pointers in the right direction is much appreciated.
I don't think you can add a button which is already an outlet of a view controller. Thinking in another way, a button(view) can only has one superview.
Create button dynamically for the title view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
iv.backgroundColor = [UIColor greenColor];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
titleButton.frame = CGRectMake(0, 0, 120, 44);
[titleButton setTitle:#"test" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[iv addSubview:titleButton];
self.navigationItem.titleView = iv;
}
//some event to change the button title
- (void)click:(UIButton *)button
{
[titleButton setTitle:#"I changed" forState:UIControlStateNormal];
}

iOS scroll view is not working

I have a button at the 1000 height pixel mark and I need a uiscrollview in order to access that button. I implemented the scrollview code below and it shows the scroll view ,but doesn't allow me to scroll down. I might be missing a key function. Any tips or advice is appreciated.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)];
scrollView.contentSize = CGSizeMake(320, 1000);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
[[self view] addSubview:scrollView];
The problem is that your button's y coordinate is 1000 pixels, as you have stated. And the y-coordinate of scrollview's contentZize is maximum upto 1000 pixels as well. So the scrollview can jus scroll up to exactly where the button starts.
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 1000, 100,45);
[scrollView addSubview:btn];
set the content size of scrollview like this
scrollView.contentSize = CGSizeMake(320, 1100);
This will surely solve it.

Creating UIScrollView In Interface Builder Using Dynamic Content

I am a newcomer to the realm of iOS development.
I am trying to get the UIScrollView control to work and ran across the following question:
steps for creating UIScrollView with Interface Builder
I followed the steps outlined in the answer for this question and everything works as I want. However, this appears to create a statically-sized view that is scrolled. What I am really after is a dynamically sized view that may or may not be scrolled. For example, instead of the view with buttons, I put a single label for which I set the text and number of lines in the viewDidLoad method. The view that contains the label is set to a static size so the scroll viewer does not attempt to scroll and the content of the label spills off of the page.
What am I missing?
you need to set te content size of the scroll
CGPoint controlsCenter = ViewBottom.center;
if(Pointsvalue > 5)
{
controlsCenter.y += txtFldFrame1.frame.origin.y+20;
ViewBottom.center = controlsCenter;
[ScrollLag addSubview:ViewBottom];
}
[ScrollLag setContentSize:(CGSizeMake(0, controlsCenter.y+100))];
make sure your label height changes on entering text.......
The URL in my comment to the original question provides a solution to the issue I raised.
I have a dynamically sized UIScrollView in a project I am working on right now. This is what I did:
self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 250, 748)];
[self.myScrollView setBackgroundColor:[UIColor blackColor]];
int heightCounter = 10;
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
newButton.frame = CGRectMake(10, heightCounter, 200, 40);
[newButton setBackgroundColor:[UIColor clearColor]];
[newButton addTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"ButtonTitle" forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.myScrollView addSubview:newButton];
heightCounter += 50;
// add additional items to the myScrollView and remember to increase the heightCounter
self.myScrollView.contentSize = CGSizeMake(250, heightCounter);
self.myScrollView.scrollEnabled = YES;
[self.view addSubview:self.myScrollView];

UIView in UIScrollView does not appear

I'm trying to create a horizontally scrollable menu similar to that in this video.
For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):
//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];
//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0);
tempBtn.frame = rect;
[buttonsView addSubview:tempBtn];
runningX = runningX + 35;
[tempBtn release];
}
[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];
You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).
Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
And make sure your view should have large content size than content size of your scrollview.It worked for me.

Resources