将UI按钮选项按钮添加到scrollview

时间:2011-12-11 09:19:53

标签: iphone ios uiscrollview uibutton

我正在尝试在滚动视图上方创建一个选项按钮,允许用户按下它并发生某些事情。

目前我有一个可滚动的地图但是如果你捏缩放它会缩放包括按钮在内的所有内容..实际上我希望按钮保持其原始位置的原始大小。

我想我必须为此发生一个新的观点,但如果我这样做,我将无法控制滚动视图...所以我想知道是否有人知道如何管理它?

这是我的代码和一些正在发生的事情的图像

- (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

解决方案:

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

1 个答案:

答案 0 :(得分:2)

不要将self.view设置为滚动视图。将滚动视图添加为self.view的子视图,并将您的按钮添加为self.view的另一个子视图。

此时您的按钮是滚动视图的子视图,因此任何滚动操作也适用于该按钮,如您所见。该按钮需要位于视图层次结构的单独分支中。