StackView不在UITableViewCell的中心

时间:2019-02-06 15:07:47

标签: ios

我想在UITableViewCell内部创建一个stackView。但是stackView不在中心。我需要以编程方式进行设置。

我创建了3个stackViews: 2 stackView-UILayoutConstraintAxisVertical与UIImageView和UILabel。 1个stackView-UILayoutConstraintAxisHorizo​​ntal和两个以上的stackViews。

请找到我的代码:

    UIView* contentView = [cell contentView];

    // Removing all the sub-views from contentView
    [[contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIStackView *stackView2= [[UIStackView alloc] init];
    stackView2.spacing = 20.0;
    [stackView2 setAxis:UILayoutConstraintAxisHorizontal];
    [stackView2 setDistribution:UIStackViewDistributionFillEqually];
    [stackView2 setAlignment:UIStackViewAlignmentCenter];
    [stackView2 setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIStackView *stackView = [[UIStackView alloc] init];
    [stackView setAxis:UILayoutConstraintAxisVertical];
    [stackView setDistribution:UIStackViewDistributionFillEqually];
    [stackView setAlignment:UIStackViewAlignmentCenter];
    [stackView setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIImageView *AscendingImage = [[UIImageView alloc] init];
    [AscendingImage setImage:MsoGetPfmImageForTCID(msotcidXLSortAscending)];
    [AscendingImage setContentMode:UIViewContentModeScaleAspectFit];

    UILabel* Label1 = [[UILabel alloc] init];
    [Label1 setText:GetAscendingKeyString()];
    Label1.translatesAutoresizingMaskIntoConstraints = false;
    Label1.textAlignment = NSTextAlignmentCenter;
    [Label1 setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]];
    [Label1 setUserInteractionEnabled:YES];

    [stackView addArrangedSubview:AscendingImage];
    [stackView addArrangedSubview:Label1];

    UIStackView *stackView1 = [[UIStackView alloc] init];

    [stackView1 setAxis:UILayoutConstraintAxisVertical];
    [stackView1 setDistribution:UIStackViewDistributionFillEqually];
    [stackView1 setAlignment:UIStackViewAlignmentCenter];
    [stackView1 setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIImageView *DescendingImage = [[UIImageView alloc] init];
    [DescendingImage setImage:MsoGetPfmImageForTCID(msotcidXLSortDescending)];
    [DescendingImage setContentMode:UIViewContentModeScaleAspectFit];

    UILabel* Label2 = [[UILabel alloc] init];
    [Label2 setText:GetDescendingKeyString()];
    Label2.translatesAutoresizingMaskIntoConstraints = false;
    Label2.textAlignment = NSTextAlignmentCenter;
    [Label2 setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]];
    [Label2 setUserInteractionEnabled:YES];

    [stackView1 addArrangedSubview:DescendingImage];
    [stackView1 addArrangedSubview:Label2];

    [stackView2 addArrangedSubview:stackView];
    [stackView2 addArrangedSubview:stackView1];

stackView2不在UITableViewCell的中心。

0 个答案:

没有答案