gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:51:01 26 4
gpt4 key购买 nike

我正在尝试添加 custom buttoncustom 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 的整个宽度。它也没有正确居中。

所以我有两个问题:

1) 如何在使用 AutoLayout 的同时将自定义按钮添加到使用容器 UIView 的整个宽度的 View 中。

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

最佳答案

因为您正在对所有其他 View 使用约束,所以我建议您对按钮做同样的事情。您可以尝试使用一些视觉布局约束来设置按钮上的框架,而不是使用静态框架

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

这应该使按钮的框架与容器 View 之一相匹配。然后你还需要做的就是在 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 ] ];
}

希望它有用!

关于ios - 使用 AutoLayout 调整 UICollectionViewCell 及其内容的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28349944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com