Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 841
Zach Ross-Clyne Avatar asked Aug 16 '26 18:08

Zach Ross-Clyne


1 Answers

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.

like image 118
Vinod Singh Avatar answered Aug 18 '26 08:08

Vinod Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!