3

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

Zach Ross-Clyne
  • 779
  • 3
  • 10
  • 35

2 Answers2

9

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.

Mert Celik
  • 665
  • 8
  • 12
Vinod Singh
  • 1,374
  • 14
  • 25
  • Still nothing... Any other ideas? – Zach Ross-Clyne Jul 30 '14 at 12:50
  • 1
    Now that works, how do I make the value a + like it would be in the bar rather than just setting the title to @"+" which is a small + – Zach Ross-Clyne Jul 30 '14 at 12:56
  • UIButtonTypeCustom doesn't work... UIButtonTypeRoundedRect does – Zach Ross-Clyne Jul 30 '14 at 12:58
  • I have used the above code to create a UIButton. When we click the button multiple times the action will not happen. This is intermittent what could be the reason for this. – Raghav Jul 13 '15 at 06:10
  • @Raghav Please make sure the user interaction is enable for your super view and your button. – Vinod Singh Jul 13 '15 at 06:22
  • Yes, the user interaction is enabled, we have navigation controller, on click it will take to next screen and on clicking the back button it will come back to main screen. If i repeat the above steps multiple times then at some moment it will nt work and instantly on next click it will work. – Raghav Jul 13 '15 at 08:32
  • @Raghav Are you have the code in ViewDidLoad or ViewwillAppear ? – Vinod Singh Jul 13 '15 at 09:04
  • @Vinod, I have used the same code which is there in the answer wrapped in a scrollview. – Raghav Jul 13 '15 at 10:26
2
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];
Ved Rauniyar
  • 1,539
  • 14
  • 21