Objective-C水平添加更多UIButton

时间:2015-07-08 10:19:35

标签: ios objective-c iphone

我使用的是开源UIAlertView。视图是

enter image description here

我想添加更多水平按钮,一个在左侧,另一个在BYE按钮右侧,就像这个一样

enter image description here

我使用的源代码是下面的

-(void)popUPView{

    UIView* contentView = [[UIView alloc] init];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    contentView.backgroundColor = [UIColor klcLightGreenColor];
    contentView.layer.cornerRadius = 12.0;

    UILabel* dismissLabel = [[UILabel alloc] init];
    dismissLabel.translatesAutoresizingMaskIntoConstraints = NO;
    dismissLabel.backgroundColor = [UIColor clearColor];
    dismissLabel.textColor = [UIColor whiteColor];
    dismissLabel.font = [UIFont boldSystemFontOfSize:32.0];
    dismissLabel.text = @"Hi.";

    UIButton* dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
    dismissButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
    dismissButton.backgroundColor = [UIColor klcGreenColor];
    [dismissButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [dismissButton setTitleColor:[[dismissButton titleColorForState:UIControlStateNormal] colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted];
    dismissButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [dismissButton setTitle:@"Bye" forState:UIControlStateNormal];
    dismissButton.layer.cornerRadius = 6.0;

    [contentView addSubview:dismissLabel];
    [contentView addSubview:dismissButton];


    NSDictionary* views = NSDictionaryOfVariableBindings(contentView, dismissButton, dismissLabel);

    [contentView addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(16)-[dismissLabel]-(10)-[dismissButton]-(24)-|"
                                             options:NSLayoutFormatAlignAllCenterX
                                             metrics:nil
                                               views:views]];


    [contentView addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(36)-[dismissLabel]-(36)-|"
                                             options:0
                                             metrics:nil
                                               views:views]];




    KLCPopup *popup = [KLCPopup popupWithContentView:contentView
                                            showType:KLCPopupShowTypeShrinkIn
                                         dismissType:KLCPopupDismissTypeShrinkOut
                                            maskType:KLCPopupMaskTypeDimmed
                            dismissOnBackgroundTouch:TRUE
                               dismissOnContentTouch:FALSE];


      [popup show];



}
你能帮助我解决这个问题吗?谢谢你提前

1 个答案:

答案 0 :(得分:0)

为此,您必须设置正确的前导/尾随值

leftView.trailing = rightView.leading

例如,如果您想要将新视图与右侧对齐,您可以执行类似这样的操作

            UIView *viewToAllign = yourView;

            UIView *view = [[UIView alloc] init];
            imageViewOn.translatesAutoresizingMaskIntoConstraints = NO;
            [self.view addSubview:view];
            NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:view
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:viewToAllign
                                                                  attribute:NSLayoutAttributeTrailing
                                                                 multiplier:1
                                                                   constant:SPACING_BETWEEN_VIEWS];

或者如果你想在左边对齐某些东西,只需切换布局属性

UIView * viewToAllign = yourView;

            UIView *view = [[UIView alloc] init];
            imageViewOn.translatesAutoresizingMaskIntoConstraints = NO;
            [self.view addSubview:view];
            NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:view
                                                                  attribute:NSLayoutAttributeTrailing
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:viewToAllign
                                                                  attribute:NSLayoutAttributeLeading
                                                                 multiplier:1
                                                                   constant:SPACING_BETWEEN_VIEWS];

然后你必须创建其他约束(高度,宽度,顶部和底部) 在此之后,您可以向容器视图添加约束