C.Johns
C.Johns

Reputation: 10245

Add UIbutton option button to scrollview

I am trying to create a options button above my scroll view that will allow the user to press it and have something happen.

Currently I have a map that is scrollable but if you pinch to zoom it zooms everything including the button.. where in actuality I would like to have the button stay as at its original size in its original position.

I think I have to make a new view for this to happen but if i do that I will not be able to control the scroll view... so I was wondering if anyone knows how I can manage this?

here is my code and some images of what is happening

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Create Scrollview
    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    scrollView.bounces = NO;

    //Create Scrolled image
    backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"auckland-300.jpg"]];
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"249a-134206f1d00-1342071f5d9.ImgPlayerAUCKLAND.png"]];

    //Initalize Button Programatically
    playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [playButton addTarget:self 
                   action:@selector(sendHttpsRequest)
         forControlEvents:UIControlEventTouchDown];
    [playButton setTitle:@"Show View" forState:UIControlStateNormal];
    playButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);


    // Note here you should size the container view appropriately and layout backgroundImage and image accordingly.
    containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,601,601)];

    //Add subviews to container
    [containerView addSubview:backgroundImage];
    [containerView addSubview:image];

    //Initalize views
    scrollView.contentSize = containerView.frame.size;
    [scrollView addSubview:containerView];
    [scrollView addSubview:playButton];

    //Scrolling
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 31.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    self.view = scrollView;    


}

enter image description here

enter image description here

Solution:

//Initalize views
    scrollView.contentSize = containerView.frame.size;
    [scrollView addSubview:containerView];

    //Scrolling
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 31.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];

    //Set up Highrachys
    [self.view addSubview:scrollView];
    [self.view addSubview:playButton];

Upvotes: 0

Views: 530

Answers (1)

jrturton
jrturton

Reputation: 119242

Don't set self.view to the scroll view. Add the scroll view as a subview of self.view instead, and add your button as another subview of self.view.

At the moment your button is a subview of the scroll view, so any scrolling action is also applying to the button, as you have seen. The button needs to be in a separate branch of the view hierarchy.

Upvotes: 2

Related Questions