Question

I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added

@property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;

in view controller handler

In the viewDidLoad method I have the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
    // You should set these.
    self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    self.mainScroll.minimumZoomScale = .5;  // You will have to set some values for these
    self.mainScroll.maximumZoomScale = 2.0;
    self.mainScroll.zoomScale = 1;

    UIButton* b = [[UIButton alloc] init];
    [b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
    [self.mainScroll addSubview:b];
}

But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.

How can I add the button in code?

Was it helpful?

Solution

Try this code

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];

OTHER TIPS

bounds and frame are not the same thing.

Use this code instead...

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];

You need to set scroll view contents size, something like the following code.

[self.mainScroll setContentSize:CGSizeMake(width, height)];

Try this :

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];

Best and easiest way to make a button from code is:

    UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [yourButton setTitle:@"Your Title" forState:UIControlStateNormal];

// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
    [yourButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];

// if you set the button's background image, button's title will be visible 
    [yourButton setBackgroundImage:[UIImage imageNamed:@"yourBackgroundImage.png"] forState:UIControlStateNormal]; 

// you can add target to the button, for handle the event when you touch it.
    [yourButton addTarget:self action:@selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside]; 

// then add the button
    [self.view addSubview:yourButton];


- (void) handleToucUpInside:(id)sender{

    // here you can do anything you want, to handle the action


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top