Adding a UIButton to UIScrollView

2019-08-17 00:29发布

I am trying to add the UIButton to UIScrollView.

I have added the UIImageView with background image *(size: 320 * 620)*. Then I added this UIImageView to UIScrollView, and It works fine.

But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).

I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.

Code :

- (void)viewDidLoad     
{    
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = TRUE;

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SingleModelVar.png"]];
    self.imageView = tempImageView;
    [tempImageView release];

    imageView.userInteractionEnabled = YES;

    scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen

    //scrollView.delaysContentTouches = NO;

    scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height);         //imageView.frame.size.height=620 here
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = 0.75;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;

    //The Problem begins ..........

    btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
    [scrollView addSubview:btnStartDate];
    //[self.view addSubview:btnStartDate];

    btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
    [scrollView addSubview:btnEndDate];
    //[self.view addSubview:btnEndDate];

    [scrollView addSubview:imageView];
    [[self view] addSubview:scrollView];
}

1条回答
Evening l夕情丶
2楼-- · 2019-08-17 00:45

it is not displayed at the top because you added them before you added the imageView. Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];

查看更多
登录 后发表回答