Lars -
Lars -

Reputation: 501

Programmatically UIButton in ScrollView

I use following code to set a button. It works fine but when setting it in a window which is scrollable the button is still fixed on the screen and the view is scrolling behind it. How do I make the button follow along with the scroll. If I set a button using the IB it does follow the scroll. And that is what I want but now I wish to use the programmatically method.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

Upvotes: 0

Views: 2171

Answers (2)

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

You add button to UIViewController's view. Change code bellow:

[self.view addSubview:button];

to following:

[scrollView addSubview:button];

Upvotes: 2

bobnoble
bobnoble

Reputation: 5824

Add the UIButton to the scroll view instead of the view controller's view. E.g., replace:

[self.view addSubview:button];

with

[myScrollView addSubview:button];

Upvotes: 0

Related Questions