iCarousel AutoLayout Multiplier issue

226 Views Asked by At

I am trying to use the multipliers on the iCarousel layout but they do not seem to have any affect at all. This is my code:

_carousel = [[iCarousel alloc]init ];
self.items = [NSMutableArray array];
for (int i = 0; i < 10; i++)
{
    [_items addObject:@(i)];
}
_carousel.type = iCarouselTypeCylinder;

_carousel.delegate = self;
_carousel.dataSource = self;

_carousel.translatesAutoresizingMaskIntoConstraints = false;

[self.view addSubview:_carousel];

[_carousel.centerXAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.centerXAnchor].active = true;
[_carousel.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor constant:-80].active = true;

[_carousel.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:40.0/100.0].active = true;
[_carousel.heightAnchor constraintEqualToAnchor:self.view.heightAnchor multiplier:30.0/100.0].active = true;

I have also noticed that if a input a constant value of the say -20 in:

[_carousel.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor constant:-20].active = true; 

the iCarousel drops well below the views bottom anchor and I don't know why?

Can anyone answer these please, forgive me if I've overlooked anything as I'am new to programmatic layouts.

1

There are 1 best solutions below

11
On

You should override and move your code to loadView to avoid having the auto layout engine generate prototyping constraints. For example:

- (void) loadView
{
   [super loadView];

   _carousel = [[iCarousel alloc]init ];
    self.items = [NSMutableArray array];
    for (int i = 0; i < 10; i++)
    {
        [_items addObject:@(i)];
    }
    _carousel.type = iCarouselTypeCylinder;

    _carousel.delegate = self;
    _carousel.dataSource = self;

     _carousel.translatesAutoresizingMaskIntoConstraints = false;

    [self.view addSubview:_carousel];

    [_carousel.centerXAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.centerXAnchor].active = true;
    [_carousel.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor constant:-80].active = true;

    [_carousel.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:40.0/100.0].active = true;
    [_carousel.heightAnchor constraintEqualToAnchor:self.view.heightAnchor multiplier:30.0/100.0].active = true;  
}

In general loadView is where you want to add subviews programmatically to a UIViewController subclass.