【问题标题】:Dealing with a position for a single UIButton in a custom UIAlertView处理自定义 UIAlertView 中单个 UIButton 的位置
【发布时间】:2013-07-26 14:02:04
【问题描述】:

我有一个很好的自定义 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];
}

【问题讨论】:

    标签: ios objective-c button position uialertview


    【解决方案1】:

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

    // 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
    

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

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 2012-01-11
      • 1970-01-01
      • 2010-10-07
      • 2011-10-14
      • 2011-05-10
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      相关资源
      最近更新 更多