我嘗試添加custom button爲custom UICollectionViewCell調整大小UICollectionViewCell,它是利用自動版式
因爲我不能在Interface Builder中添加自定義按鈕(需要用特定的方法來ALLOC吧)的內容,我加入一個UIView作爲一個佔位符,它有一個清晰的背景(在這個問題中用於演示黑色)。
這是它的樣子:
然後在自定義類代碼爲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;
}
}
但你可以看到自定義按鈕沒有得到containerView的全部寬度。它也沒有正確居中。
所以我有2個問題:
1)如何添加自定義按鈕,它使用了容器的UIView的全寬視圖,而使用自動版式。
2)有沒有一種方法可以將空白添加到卡的左側和右側?
您在第一個問題中說「使用自動佈局時」,但沒有在按鈕和其容器之間使用自動佈局。不要設置按鈕的框架,而應將其固定到容器視圖的所有四邊,並帶有約束條件。 – rdelmar 2015-02-05 17:28:38
@rdelmar謝謝你的回答。我怎麼能這樣做? – Galip 2015-02-05 17:48:00