2016-06-11 67 views
0

我想在iCarousel布局上使用乘数,但他们似乎没有任何影响。这是我的代码:iCarousel AutoLayout乘数问题

_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; 

我也注意到,如果输入的恒定值的说,在-20:

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

的iCarousel下降到大大低于意见底部锚和我不不知道为什么?

任何人都可以回答这些请原谅我,如果我忽略了任何东西,因为我是新的编程布局。

回答

0

您应该覆盖并将代码移动到loadView以避免让自动布局引擎生成原型制作约束。例如:

- (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; 
} 

一般loadView是要编程子视图添加到UIViewController子类。

+0

谢谢你:)你能帮我解答这个问题吗? – sharp

+0

你试过这段代码吗?这种固定的自动布局在运行代码时会遇到问题,因为原型约束会导致自动布局约束中断。我在你的'UIViewController'或'UIView'生命周期中发布的代码在哪里? – beyowulf

+0

我没有使用IB,所以没有原型制约。这些代码块(constraintEqualToAnchor:乘数方法)在程序中的其他地方正常工作,但不在iCarousel上。我不能将这段代码放在'loadView'中,因为在我的程序中,iCarousel的设置发生在视图加载并基于条件之后。 iCarousel由centerX和bottom锚定位,但乘数没有影响,导致iCarousel在所有设备上的点数相同,这是我不想要的。我希望它在不同的屏幕尺寸上相应地调整大小 – sharp