UIScrollView Center the View in Long Width - ios

I have a UIScrollView in my ViewController that has 1600Width and 600Height. i Put 3 images there so the swiping from left to right is nice until the each end. My problem is in the 2nd photo in the middle. i can;t make the scrolling stop on exactly on it.. how can i make the UIVScrollView centered?
I used this code to scroll from out of bounds
[self.myScrollView setScrollEnabled:YES];
[self.myScrollView setContentSize:CGSizeMake(1600, 600)];
Please help me through this. your help will be very much appreciated.

-(void)creatScrollViewWithimage
{
UIScrollView *mainScrollview=[[UIScrollView alloc]initWithFrame:self.view.frame];
mainScrollview.pagingEnabled=YES;
for (int i=0; i<3; i++) {
CGRect newFrame=mainScrollview.frame;
newFrame.origin.x=mainScrollview.frame.size.width*i;
UIView *view=[[UIView alloc]initWithFrame:newFrame];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.frame];
[imageView setImage:[UIImage imageNamed:#"imagename.png"]];
[view addSubview:imageView];
[mainScrollview addSubview:view];
}
[mainScrollview setContentSize:CGSizeMake(3*mainScrollview.frame.size.width, mainScrollview.frame.size.height)];
[self.view addSubview:mainScrollview];
}
this will create scroll view for entire screen and add three UIImageView with paging enabled.

Related

UIButton not Responsive When placed out of its container UIScrollView

I have a UIScrollView. Inside this UIScrollView is a UIView which contains every other child views (including a UIButton). It has the same bounds as the UIScrollView.
I want:
The bottom of the UIScrollView is not scroll-able (where the UIButton is placed).
The UIButton is responsive.
So I set the bottom of the UIScrollView a little higher than the UIVIew as below image:
This will make the UIButton not responsive (it's responsive in the left layout). Any suggestion? Thanks!
Some part of my code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
Update:
Looks like the right way is to keep the bottom of the scroll view and set its contentSize, but since I'm using autolayout, I found that the height of the contentSize is always zero???
You need to place the button in a view that is outside of the scroll views view hierarchy.
Currently the hit testing is failing because the button is outside of the scroll views bounds but within its hierarchy.
Add the button into scroll view after that try the below code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];
self.scrollView.delaysContentTouches = NO; //Add this line
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];

ScrollView shifting upward while tapping on it

I am new to iPhone development. I have a image in a UIImageView contained within UIScrollView(zoomscrollView) which is contained within another UIScrollView(scrollViewgalery).
My problem is that while on touching the first scrollview i.e. zoomScrollview, it moves towards upward direction.
I Hope you need a view similar to photos gallery view. Here u will need a main scrollviewGallery and inside it your scrollviewZoom whose height should be same as scrollViewGallery for horizontal scroll and scrollViewZooms width should be same as scrollViewGallery for vertical scroll. Also Make sure u have enabled pagination for scrollViewGallery.
Your sample code goes here
[_scrollView setDelegate:self];
int index=0;
for (NSString* stringImageName in self.arrayGalleryImages)
{
UIScrollView *imgScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(index*CGRectGetWidth(_scrollView.frame),0,CGRectGetWidth(_scrollView.frame),CGRectGetHeight(_scrollView.frame))];
[imgScroll setDelegate:self];
[imgScroll setTag:index];
[imgScroll setScrollEnabled:NO];
[imgScroll setMinimumZoomScale:1.0];
[imgScroll setMaximumZoomScale:3.0];
[imgScroll setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
UIButton *btnGallery=[[UIButton alloc] initWithFrame: imgScroll.bounds];
[btnGallery addTarget:self action:#selector(gotoFullScreenGallery) forControlEvents:UIControlEventTouchUpInside];
[btnGallery setAdjustsImageWhenHighlighted:NO];
[btnGallery setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[btnGallery setTag:index];
[[btnGallery imageView] setContentMode: UIViewContentModeScaleAspectFit];
[btnGallery setImage:[UIImage imageNamed:stringImageName] forState:UIControlStateNormal];
[imgScroll addSubview:btnGallery];
[_scrollView addSubview:imgScroll];
index++;
}
[_scrollView setContentSize:CGSizeMake(index*CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
Assuming arrayGalleryImages consists of gallery image names.
I used button to add more action on tap.
Try code and manage scrollview delegate methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if([previouslyZoomedScroll zoomScale]!=1.0)
{
[previouslyZoomedScroll setZoomScale:1.0];
}
}

ios ScrollView empty blank area does not scroll

I have created a scrollview programmatically in ios.
I am displaying 2-3 images on that scrollview and adding images vertically one below other.
When I touch on the image and scroll vertically it works. But if I touch the empty space between the images and try to scroll it does not work. Scrollview does not respond to that.
Here is the relevant code :
UIImageView* imageView = [[UIImageView alloc] initWithFrame:imageRect];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self didAddSubview:imageView];
.....
......
[scrollView setScrollEnabled:YES];
[[self view] addSubview:scrollView];
Any inputs will be helpful.
Thanks.
You can set the content size of scrollView use this code hope it will help you :-
- (void) viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[scrollView setScrollEnabled:YES];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 250, 200)];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(320, 600);
}
Set,
[scrollView setFrame:size];
[scrollView setContentSize:contentsize];
If it will not scroll vertically or horizontally then you have to change content size it should be greater then current size.
This question is old but I was having the exact same issue with my scrollview, and this was the only page I found about this all over the internet.
I was using a content view inside scroll view, and I just set a background color on my contentView and it worked.
Content size was being set automatically throw auto layout, so no need to set it manually.

UIScrollView does not scroll

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?

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