Zach Ross-Clyne
Zach Ross-Clyne

Reputation: 799

Adding UIButton Programmatically won't show

I'm currently making an iOS 8 application ready for the app store upon iOS 8 release, I don't obviously I cannot show much code as I'm keeping the project very quiet but I am creating a view controller with a scroll view and many objects throughout to create a profile for the user, however I'm trying to add a button to all the user to add projects to their profile and the button won't get added to the scroll view... Is there an unwritten rule with UIButtons and UIScrollViews?

Heres the code I'm using to create the button

UIButton *addProject = [[UIButton alloc] initWithFrame:CGRectMake(210, 285, 100, 18)];
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];

Is there something I need to do differently to add it to the scroll view?

EDIT 1: _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 699);

EDIT 2: Also, the scroll view is created in the storyboard and has a frame of 0, 64, 320, 455 but the content size as above has a larger height

Upvotes: 3

Views: 18617

Answers (2)

Ved Rauniyar
Ved Rauniyar

Reputation: 1589

UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];
_scrollView.backgroundColor = [UIColor clearColor];

Upvotes: 2

Vinod Jat
Vinod Jat

Reputation: 1374

Try with this;

UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];

Try to add this line;

addProject.backgroundColor = [UIColor redColor];

It's some time happens that it may not appear due to same background color.

Upvotes: 9

Related Questions