2016-06-11 93 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