0

我有一个很好的自定义 UIAlertView 的功能,它与 2 个按钮完美搭配,但问题是,我怎么知道只有 1 个按钮而不是 2 个按钮?或者我该如何解决只有 1 个按钮的定位问题?

我的代码是:

- (void)layoutSubviews
{
    for (id obj in self.subviews) //Search in all sub views
    {
        if ([obj isKindOfClass:[UIImageView class]])
        {
            UIImageView *imageView = (UIImageView*)obj;
            float width = 284.0;
            float height = 141.0;

            CGSize size = CGSizeZero;
            size.width = width;
            size.height = height;

            UIImage *newImage = [self imageWithImage:[UIImage imageNamed:@"CustomAlertView"] scaledToSize:size];
            imageView.image = newImage;
            [imageView sizeToFit];

        }

        if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
        {
            UILabel *label = (UILabel*)obj;

            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor blackColor];
            label.shadowColor = [UIColor clearColor];
            label.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
        }

        if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
        {
            UIButton *button = (UIButton*)obj;

            CGRect buttonFrame = button.frame; // Change UIButton size
            buttonFrame.size = CGSizeMake(127, 35);
            CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
            buttonFrame.origin.y = newYPos;
            button.frame = buttonFrame;

            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

            switch (button.tag)
            {
                case kFIRST_BUTTON:
                {
                    [button setBackgroundImage:[UIImage imageNamed:@"short_orange_button_off"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"short_orange_button_on"] forState:UIControlStateHighlighted];
                }
                    break;
                case kSECOND_BUTTON:
                {
                    [button setBackgroundImage:[UIImage imageNamed:@"short_button_black_off"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"short_button_black_on"] forState:UIControlStateHighlighted];
                }
                    break;
            }
        }
    }

    [self updateConstraints];
}
4

1 回答 1

0

好吧,自己想办法,我补充说:

// Determine how many buttons we deal with
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        for (id obj in self.subviews)
        {
            if ([obj isKindOfClass:[UIButton class]])
            {
                self.numOfButtons++;
            }
        }

        NSLog(@"self.numOfButtons: %d", self.numOfButtons);
    });

紧接着:

- (void)layoutSubviews

然后我知道我处理了多少个按钮。

于 2013-07-26T14:25:48.153 回答