0

我嘗試添加custom buttoncustom UICollectionViewCell調整大小UICollectionViewCell,它是利用自動版式

因爲我不能在Interface Builder中添加自定義按鈕(需要用特定的方法來ALLOC吧)的內容,我加入一個UIView作爲一個佔位符,它有一個清晰的背景(在這個問題中用於演示黑色)。

這是它的樣子:

enter image description here

然後在自定義類代碼爲UICollectonViewCell我做的:

- (void)awakeFromNib { 
// Initialization code 
self.btnAddOrRemove = [[HTPressableButton alloc] initWithFrame:self.btnContainerView.bounds buttonStyle:HTPressableButtonStyleRounded]; 
self.btnAddOrRemove.center = CGPointMake(self.center.x, self.btnAddOrRemove.center.y); 
//self.btnContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
} 

- (void)layoutSubviews 
{ 
[super layoutSubviews]; 

CGRect bounds = self.bounds; 
if (self.shadowWidth != bounds.size.width) 
{ 
    if (self.shadowWidth == 0) 
    { 
     [self.layer setMasksToBounds:NO ]; 
     [self.layer setShadowColor:[[UIColor blackColor ] CGColor ] ]; 
     [self.layer setShadowOpacity:0.5 ]; 
     [self.layer setShadowRadius:5.0 ]; 
     [self.layer setShadowOffset:CGSizeMake(0 , 0) ]; 
     self.layer.cornerRadius = 5.0; 
    } 
    [self.layer setShadowPath:[[UIBezierPath bezierPathWithRect:bounds ] CGPath ] ]; 

    if ([ChosenCategory getInstance].doesExist) { 
     if ([ChosenCategory getInstance].category == self.ingredientCategory) { 
      [self.btnAddOrRemove setTitle:NSLocalizedString(@"Edit profile", nil) forState:UIControlStateNormal]; 
      [self.btnAddOrRemove setButtonColor:[UIColor ht_mediumColor]]; 
      [self.btnAddOrRemove setShadowColor:[UIColor ht_mediumDarkColor]]; 
      [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateNormal]; 
      [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateHighlighted]; 
      [self.btnAddOrRemove addTarget:self action:@selector(toDetail:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     else { 
      [self.btnAddOrRemove setTitle:NSLocalizedString(@"Choose Profile", nil) forState:UIControlStateNormal]; 
      [self.btnAddOrRemove setButtonColor:[UIColor ht_bitterSweetColor]]; 
      [self.btnAddOrRemove setShadowColor:[UIColor ht_bitterSweetDarkColor]]; 
      [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateNormal]; 
      [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateHighlighted]; 
      [self.btnAddOrRemove addTarget:self action:@selector(chosenProfile:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
    } 
    else { 
     [self.btnAddOrRemove setTitle:NSLocalizedString(@"Choose profile", nil) forState:UIControlStateNormal]; 
     [self.btnAddOrRemove setButtonColor:[UIColor ht_mediumColor]]; 
     [self.btnAddOrRemove setShadowColor:[UIColor ht_mediumDarkColor]]; 
     [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateNormal]; 
     [self.btnAddOrRemove setTitleColor:[UIColor ht_ashColor] forState:UIControlStateHighlighted]; 
     [self.btnAddOrRemove addTarget:self action:@selector(chosenProfile:) forControlEvents:UIControlEventTouchUpInside]; 
    } 

    [self.btnContainerView addSubview:self.btnAddOrRemove]; 

    self.shadowWidth = bounds.size.width; 
    self.lblSummary.preferredMaxLayoutWidth = self.lblSummary.frame.size.width; 
} 
} 

enter image description here

但你可以看到自定義按鈕沒有得到containerView的全部寬度。它也沒有正確居中。

所以我有2個問題:

1)如何添加自定義按鈕,它使用了容器的UIView的全寬視圖,而使用自動版式。

2)有沒有一種方法可以將空白添加到卡的左側和右側?

+0

您在第一個問題中說「使用自動佈局時」,但沒有在按鈕和其容器之間使用自動佈局。不要設置按鈕的框架,而應將其固定到容器視圖的所有四邊,並帶有約束條件。 – rdelmar 2015-02-05 17:28:38

+0

@rdelmar謝謝你的回答。我怎麼能這樣做? – Galip 2015-02-05 17:48:00

回答

1

因爲你在所有其他視圖上使用約束,我建議你對按鈕做同樣的操作。而不是使用靜態框架,你可以嘗試使用一些視覺上的佈局限制設置你的按鈕

- (void)awakeFromNib 
{ 

    self.btnAddOrRemove = [[HTPressableButton alloc] initWithFrame:self.btnContainerView.bounds buttonStyle:HTPressableButtonStyleRounded]; 
    [self.btnContainerView addSubView:self.btnAddOrRemove]; 
    [self.btnAddOrRemove setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.btnAddOrRemove addConstraintsToFillParentHorizontally:self.btnContainerView] 
    [self.btnAddOrRemove addConstraintsToFillParentVertically:self.btnContainerView] 
} 

- (void)addConstraintsToFillParentHorizontally:(UIView *)parentView 
{ 
    [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{ @"view": self }]]; 
} 

- (void)addConstraintsToFillParentVertically:(UIView *)parentView 
{ 
    [parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{ @"view": self }]]; 
} 

幀這應該使按鈕的幀匹配容器視圖的一個。然後,你仍然需要做的是在layoutSubviews方法中應用圓角。

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    if (self.shadowWidth == 0) 
    { 
     [self.layer setMasksToBounds:NO ]; 
     [self.layer setShadowColor:[[UIColor blackColor] CGColor]]; 
     [self.layer setShadowOpacity:0.5 ]; 
     [self.layer setShadowRadius:5.0 ]; 
     [self.layer setShadowOffset:CGSizeMake(0, 0)]; 
     self.layer.cornerRadius = 5.0; 
    } 
    [self.layer setShadowPath:[[UIBezierPath bezierPathWithRect:bounds ] CGPath ] ]; 
} 

希望它有效!

+0

感謝您的回答,但我選擇實現了一個不同的庫,使我能夠在Interface Builder中聲明自定義按鈕,因此問題不再存在。我的第二個問題仍然存在,但(如何減少自定義'卡'的寬度) – Galip 2015-02-09 14:02:51

1

其實你可以離開這個按鈕,因爲它是,如果你想,只要將佔位視圖背景顏色爲透明

UIColor.clearcolor() 

和居中的底部放UIButton矩形同樣大小的佔位符視圖,然後設置內容你的按鈕模式進入背景圖像裏面!

contentMode = UIViewContentMode.Center