Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
Hi at all, I have a problem with auto layout, I don't understand how can I create a xib with some constraints, and then load this xib into a view and create a scrollview with this view, this is my code:

@implementation TutorialPage


TutoialPage.m
Code:
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {   UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"TutorialPage" owner:self options:nil] objectAtIndex:0];
        [view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:view];
     }
}

this is my TutorialViewControlelr.m
Code:
- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];
    if (isFirst)
    {
        [self initViews];
        [self initConstraints];
        [self initTutorial];
       
        isFirst = NO;
    }
}
-(void) initViews
{
    _scrollView = [[UIScrollView alloc] init];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setBackgroundColor:[UIColor clearColor]];
    [_scrollView setDelegate:self];
   
    _scrollViewContent = [[UIView alloc] init];
    [_scrollViewContent setBackgroundColor:[UIColor clearColor]];

    [_scrollView addSubview:_scrollViewContent];
   
    [self.view addSubview:self.scrollView]; 
}
-(void) initConstraints
{
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    _scrollViewContent.translatesAutoresizingMaskIntoConstraints = NO;
    id views = @{
                 @"_scrollView": _scrollView,
                 @"_scrollViewContent": _scrollViewContent
                 };
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:views]];
   
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollViewContent]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollViewContent]|" options:0 metrics:nil views:views]];
}
- (void) initTutorial { 
    [self.view layoutIfNeeded];
   
    int i = 0;
    for (V10Page *page in pagineTutorial)
    {
        TutorialPage *tutorial = [[TutorialPage alloc] initWithFrame:CGRectMake(i * [Utils getScreenSize].width, 0,[Utils getScreenSize].width, _scrollView.frame.size.height)];
        [_scrollViewContent addSubview:tutorial];
       
        i++;
    }
}
- (void) viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];
   
    if (pagineTutorial.count > 0)
    {
        [_scrollViewContent setFrame:CGRectMake(0, 0, _scrollView.frame.size.width * pagineTutorial.count, _scrollView.frame.size.height)];
        [_scrollView setContentSize:_scrollViewContent.frame.size];
    }
    [self.view layoutIfNeeded];
}

where is the mistake? the problem is that size class seems to don't exist, if I edit a constraint of iPad class size nothin appends, only If I change AnyAny constraint I See any changes...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.