使用AutoLayout调整UICollectionViewCell及其内容的大小

时间:2015-02-05 17:08:49

标签: ios objective-c autolayout uicollectionview uicollectionviewcell

我正在尝试将custom button添加到custom UICollectionViewCell

因为我无法在Interface Builder中添加自定义按钮(需要使用特定方法分配它),所以我添加了一个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)在使用AutoLayout时,如何将自定义按钮添加到使用容器UIView的整个宽度的视图中。

2)有没有办法在卡片的左右两侧添加空格?

2 个答案:

答案 0 :(得分: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 ] ];
}

希望它有效!

答案 1 :(得分:1)

实际上,如果您只想将占位符视图背景颜色设置为透明,则可以保持按钮不变

UIColor.clearcolor()

并将底部放置UIButton矩形相同尺寸的占位符视图,然后设置按钮的内容模式以在其中输入背景图像!!!

contentMode = UIViewContentMode.Center