ScrollView shifting upward while tapping on it - ios

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];
}
}

Related

My UIView has a UIButton as one of it's subviews but not responding to events in objective-c

Here is my code. It has started to look crowded after an hour or 2 of trying to solve this issue. I've tried setting user interaction enabled to yes, I've tried just using a button alone and not making it the subview of another view.
Right now I have filterBar > filterBarContainer > filterButton.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
// Create filter bar with specified dimensions
UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[filterBar setUserInteractionEnabled:YES];
// Make it a subview of navigation controller
[[[self navigationController] navigationBar] addSubview:filterBar];
[filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
[filterBar setTag:1];
// Reusable vars
// CGFloat filterBarX = filterBar.frame.origin.x;
//CGFloat filterBarY = filterBar.frame.origin.y;
CGFloat filterBarHeight = filterBar.frame.size.height;
CGFloat filterBarWidth = filterBar.frame.size.width;
// Create centre filter button with specified dimensions
UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
[filterButtonContainer setUserInteractionEnabled:YES];
// Make it a subview of filter bar
[filterBar addSubview:filterButtonContainer];
[filterButtonContainer setBackgroundColor:[UIColor redColor]];
// Centre the button
[filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];
// Add button to filter button
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[filterButton setUserInteractionEnabled: YES];
[filterButton setFrame:CGRectMake(0, 0,40 , 20)];
[filterButton setTitle:#"test" forState:UIControlStateNormal];
[filterButtonContainer addSubview:filterButton];
[filterButton setBackgroundColor:[UIColor yellowColor]];
// self.filterButton = filterButton;
[filterButton addTarget:self action:#selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"dd");
filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
filterButtonContainer.layer.borderWidth = 1;
This is the method I'm trying to trigger.
- (void)filterButtonTapped:(UIButton *)sender
{
NSLog(#"filter button tapped");
UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:#"Filter Garments"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Recommended", #"What's New", #"Price - High to Low", #"Price - Low to High", nil];
[filterOptions showInView:self.collectionView];
}
Most likely one of your parent views (superviews of your button) have a width or height that places the filter button outside of their area. The button is still displayed (if you don't define that the view should cut off subviews), but elements outside the view's area will not get any touch events. Check your superview's width and height.
You're adding filterBar as a subview of your navigationBar, but you're setting it's y-origin within navigationBar to 42. And then you're setting your filterButtonContainer's y-origin within the filterBar to 2. Assuming navigationBar's height is 44, most of the filterBar and the entirety of your filterButtonContainer and filterButton won't be within their navigationBar superview so the button wouldn't be selectable.
Edit: To make it selectable while keeping the positioning intact, I suggest adding the filter bar to self.view. For example, change:
[[[self navigationController] navigationBar] addSubview:filterBar];
to
[self.view addSubview:filterBar];

UIScrollView Center the View in Long Width

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.

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?

How to add a button to a UIScrollView as a subView from outside the same method?

So I am trying to get a simple UIScrollView working with a bunch of buttons. I was successfully able to accomplish this task with the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(320, 1487)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -500, 320, 2800)];
[imgView setBackgroundColor:[UIColor blackColor]];
[_scroller addSubview:imgView];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:#"small.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(btn1_m) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn1];
However, what if I to add buttons through interface builder instead of programmatically adding them? Whenever I do this, the scroller no longer scrolls!
For example, if I use the following code after control-dragging a button:
- (IBAction)theButton:(UIButton *)sender {
[_scroller addSubview:sender];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(320, 1487)];
}
...The scroll doesn't work, I see the button but nothing scrolls anymore. Basically, I just want to use interface builder to add buttons to my app and have them scroll too, but can't seem to get this to work!
When you create your UI programmatically, the subviews of your scrollview are like this:
ImageView
Button
Button
...
When you use IB to add your buttons, you end up with this:
Button
Button
...
ImageView
Place a breakpoint after your image view is added to the scroll view. In the LLDB console type:
(lldb) po _scroller.subviews
I suspect you will see your image view at the bottom of the list. It is covering your buttons, but it is scrolling, it just doesn't look like it as it is a large black image view.
Send your image view to the back of the subview list:
[_scroller sendSubviewToBack:imgView];
Then you will see your buttons again.
Also, after you have done this, confirm your scrollview content size in lldb:
(lldb) p _scroller.contentSize
If it all looks correct, it should be scrolling.

scroll view + segment button +iOS

I have 2 scroll views. I put many buttons onto the first scroll view. When I click one button in the first scroll view, related buttons fill the second scroll view.
I want the buttons in the first scroll view to have the effect as if they are segment control. i.e. when one button is clicked, its image becomes dim and can't be clicked again until other buttons in the first scroll vew are clicked.
How to achieve this? any sample code is appreciated! thanks!
Can't you just keep track of which button was last clicked, and reset that one to be in the active state, and set the one you're now clicking to the inactive state? Just create a property, lastClicked, and do this:
-(IBAction) buttonClicked:(UIButton *)sender {
[self.lastClicked setUserInteractionEnabled:YES];
[self.lastClicked setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
[sender setUserInteractionEnabled:NO];
[sender setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
self.lastClicked = sender;
}
Try adding a segmented control inside a scroll view.
- (void)viewDidLoad
{
[super viewDidLoad];
journals = [[NSMutableArray alloc]init];
self.tableView.dataSource = self;
self.tableView.delegate = self;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 49, 320, 29)];
self.segmentedControl.frame = CGRectMake(0, 0, 640, 29);
scrollView.contentSize = CGSizeMake(self.segmentedControl.frame.size.width, self.segmentedControl.frame.size.height -1);
scrollView.showsHorizontalScrollIndicator = NO;
self.segmentedControl.selectedSegmentIndex = 0;
[scrollView addSubview:self.segmentedControl];
[self.view addSubview:scrollView];
[self fillJournals];
// Do any additional setup after loading the view, typically from a nib.
}
Super simple, very ugly, but it works. You could optimize this out further by having it check the image and the interaction state of the button, so you aren't setting an image every time this loops through.
-(void) buttonClicked:(UIButton *)sender {
for (UIButton *btn in self.scrollView.subviews) {
if ([sender isKindOfClass:[UIButton class]]) {
if (btn == sender) {
[btn setUserInteractionEnabled:NO];
[btn setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
} else {
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
}
}
}
}

Resources