2

3つのUIImageを含むUIViewがあります。(絵コンテを使って描いた)

常に3つのUIImageを表示するとは限りません。私はそれらを次のように非表示にして表示します:

facebookIcon.hidden = YES/NO;

それらの間の正確な間隔を表示したいと思います。

たとえば、画像が1つある場合は、中央に表示されます。

画像が2つある場合、中央の両側に1つずつあります

画像が3つある場合、右側に1つ、中央に1つ、左側に1つあります。

UITabViewControllerアイコンに似ています-正確なスペースで自動的に広がります。

どうすればいいですか?

4

3 に答える 3

4

自動レイアウトまたはを使用せずにautoResizeMask。最善の方法でコーディングされていない場合は、適切な変更を加えてください。これはself.view、がimageviewsのスーパービューであり、すべての画像ビューが同じサイズであることを前提としています。

    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

まもなく:iOS6で自動レイアウトを使用する

または、 iOS6より前のマスクの自動サイズ変更

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

InterfaceBuilderですべての自動レイアウト/サイズ変更マスクを実行できることに注意してください。

于 2013-02-27T16:30:14.367 に答える