1

I have a custom view with height 800 in XIB as seen on the picture :

enter image description here

this is the hierarchy :

enter image description here

this the code on my customView.m :

- (id)initWithFrame:(CGRect)frame
{
NSLog(@"initWithFrame");
self = [super initWithFrame:frame];
if (self) {
    [self setup];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"initWithCoder");
self = [super initWithCoder:aDecoder];
if(self) {
    [self setup];
}
return self;
}

- (void)setup {

[[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil];
[self addSubview:self.view];
self.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, CGRectGetHeight(self.view.frame));
//[self.myscrollview setContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, 2000)];
NSLog(@"contentSize Height :%f", self.myscrollview.contentSize.height);
NSLog(@"contentView Height :%f", self.contentView.frame.size.height);
}

and addSubview the customView on my viewController view :

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

customView *myView = [[customView alloc] init];

[self.view addSubview:myView];

}

it is loaded, but the scrollView is not scrolling. Then I check the customView and contentView height it returns 800 but the scrollView contentSize height it returns 0. so I tried to set the contentSize to 2000 but it still not scrolling. the thing that made me confused is that why after I set the scrollView trailing, leading, bottom and top 0 to superView it is not following the contentView height, instead return 0. How am i supposed to set the contentSize then?

nb: the contentView has height and width equal to superView and the priority set to 250

github : project example

mfaani
  • 33,269
  • 19
  • 164
  • 293
xeravim
  • 463
  • 7
  • 20
  • Add proper constraint to view when you add it as subview. that will solve your problem. https://stackoverflow.com/questions/35294571/how-to-add-constraint-programmatically-in-objective-c/35317238 – PlusInfosys Oct 08 '18 at 04:39
  • i am not doing constraint on code. – xeravim Oct 08 '18 at 05:10
  • But you should. everything you have at design time is with constraint. and if you dont apply constraint to view added runtime, it can never give you result you want. – PlusInfosys Oct 08 '18 at 05:25

1 Answers1

1

It's because you didn't set frame for myView. Replace your viewDidLoad method with below code and it will work as expected

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
    [self.wantToShowHereView addSubview:customV];
}
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • Thank you! its working not what I expected since the scroll is not scrolling until the last object below, and its super lagi, do you know why? – xeravim Oct 08 '18 at 05:10
  • What is the last object below? The scroll view in your sample contains only 1 UILabel and it's smooth – trungduc Oct 08 '18 at 05:14