2

我有 UIView,里面有 3 个 UIImages。(我用故事板来画它们)

我并不总是显示 3 个 UIImages。我像这样隐藏和显示它们:

facebookIcon.hidden = YES/NO;

我想显示它们之间的准确间距。

例如,如果有一个图像 - 它将位于中心。

如果有 2 个图像 - 中心的每一侧都会有一个

如果有 3 张图像 - 将有一张在右侧,一张在中间,一张在左侧。

类似于UITabViewController图标 - 使用准确的空格自动传播。

我该怎么做?

4

3 回答 3

4

不使用自动布局或autoResizeMask. 没有以最佳方式编码,请进行适当的更改。这假设self.view是图像视图的超级视图,并且所有图像视图的大小相同。

    CGFloat padding = 10; //your wish
    NSMutableArray *notHidden = [NSMutableArray array];
    for (UIImageView *img in self.view.subviews) {
        if (!img.hidden) {
            [notHidden addObject:img];
        }
    }
    int arrayCount = notHidden.count;
    switch (arrayCount%2) {
        case 0:{
            for (int index =-arrayCount/2;index<arrayCount/2;index++) {
                int tempIndex =index;
                UIImageView *img = (UIImageView *)[notHidden objectAtIndex:index+arrayCount/2];
                CGRect tempFrame = img.frame;

                if (index>=0) {
                    tempIndex=index+1;
                }
                CGFloat xDiff = img.frame.size.width*index+padding*tempIndex;


                tempFrame.origin.x = self.view.center.x+xDiff;
                [[notHidden objectAtIndex:index+arrayCount/2] setFrame:tempFrame];
            }


            break;  
        }
        case 1:{
            for (int index =-arrayCount/2;index<=arrayCount/2;index++) {
                int tempIndex =-1;
                UIImageView *img = (UIImageView *)[notHidden objectAtIndex:index+arrayCount/2];
                CGRect tempFrame = img.frame;
                CGFloat xDiff = img.frame.size.width*index+padding*index+tempIndex*(img.frame.size.width/2);


                tempFrame.origin.x = self.view.center.x+xDiff;
                [[notHidden objectAtIndex:index+arrayCount/2] setFrame:tempFrame];
            }
        }
        default:
            break;
    }
于 2013-02-27T17:18:07.317 回答
0

解决方案如下:

- (void)showIconsInView:(NSArray *)channelIcons iconsView:(UIView *)iconsView
{
    int iconsCounter = channelIcons.count;
    int iconWidth = 13;
    int iconHeight = 12;
    int space = (iconsView.frame.size.width - (iconWidth*iconsCounter))/(iconsCounter+1);
    //add the icon image to the view
    NSString *icon = nil;
    NSEnumerator *enumerator = [channelIcons objectEnumerator];
    while (icon = (NSString*)[enumerator nextObject]) {
        UIImage *image = [UIImage imageNamed:icon];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.frame = CGRectMake(space, 0, iconWidth,iconHeight);
        space = space*2 +iconWidth;
        [iconsView addSubview:imageView];
    }
}
于 2013-03-18T06:29:42.877 回答
0

很快:在 iOS 6 中使用自动布局

或在 iOS 6 之前自动调整大小蒙版

// auto resizing masks
self.button1.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.button2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.button3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

请注意,您可以在 Interface Builder 中执行所有自动布局/调整大小的蒙版。

于 2013-02-27T16:30:14.367 回答