0

i am trying to add button inside the scrollview with center constrain, constrain working but scroll not working scroll become stuck, Anyone can help me where is mistake,

-(void)viewDidLoad{
scrllview = [[UIScrollView alloc] initWithFrame:
                 [[UIScreen mainScreen] applicationFrame]];
    scrllview.backgroundColor = [UIColor orangeColor];
    self.view=scrllview;
    [scrllview setContentSize:CGSizeMake(300, 1000)];


    submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitButton setTitle:@"connect" forState:UIControlStateNormal];
    [submitButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]];
    [submitButton addTarget:self
                     action:@selector(myMethod:)
           forControlEvents:UIControlEventTouchUpInside];
    submitButton.backgroundColor = [UIColor blackColor];
    submitButton.translatesAutoresizingMaskIntoConstraints = NO ;
    [scrllview addSubview:submitButton];

    NSLayoutConstraint *constraint = [NSLayoutConstraint
                                      constraintWithItem:submitButton
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:scrllview
                                      attribute:NSLayoutAttributeCenterX
                                      multiplier:1.0f
                                      constant:0.0f];

    [scrllview addConstraint:constraint];

    constraint = [NSLayoutConstraint
                  constraintWithItem:submitButton
                  attribute:NSLayoutAttributeCenterY
                  relatedBy:NSLayoutRelationEqual
                  toItem:scrllview
                  attribute:NSLayoutAttributeCenterY
                  multiplier:1.0f
                  constant:0.0f];

    [scrllview addConstraint:constraint];
}
Bandish Dave
  • 791
  • 1
  • 7
  • 27

1 Answers1

1

While you are using scrollview with autolayout, you should not use contentsize.

You can create a view inside scroll view, which should have your required height and constraint relative to scrollview. You can add your button inside that view.

It will work. I have implemented it with xib. Same issue while adding control directly in scrollview. I used view inside scrollview and added required control to view.

Auto layout UIScrollView with subviews with dynamic heights

Community
  • 1
  • 1
Sanjay Ghinaiya
  • 146
  • 1
  • 5
  • thx for replaying...can you suggest me any example or link,because last 5 hr i have already working on this but still not implemented. – Bandish Dave Sep 06 '14 at 13:16
  • I didn't found any example or link on net. But i'm sure, It will work as i said. I implemented in one of the product, so i can't share code. Can you send me sample code which doesn't work? I will fix it and send you update. – Sanjay Ghinaiya Sep 06 '14 at 13:38
  • i used only one button in scrollview and gave contain size of scrollview.plz edit and make correct my code above. – Bandish Dave Sep 06 '14 at 13:46
  • can you give required height of that button? Also self.view=scrllview;? it should be [self.view addSubview:scrllview]; – Sanjay Ghinaiya Sep 06 '14 at 13:52
  • as u wish u can take but main purpose is scroll will be enable and button must be centre. – Bandish Dave Sep 06 '14 at 13:56
  • scrollview should have more content than it's size for scroll. do you want to me take it at bottom or height should be more than screen height? – Sanjay Ghinaiya Sep 06 '14 at 14:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60751/discussion-between-sanjay-ghinaiya-and-bandish-dave). – Sanjay Ghinaiya Sep 06 '14 at 14:01