Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIView中图像之间的精确间距与UITabViewController中的相同_Iphone_Objective C_Xcode_Cocoa Touch_Uiview - Fatal编程技术网

Iphone UIView中图像之间的精确间距与UITabViewController中的相同

Iphone UIView中图像之间的精确间距与UITabViewController中的相同,iphone,objective-c,xcode,cocoa-touch,uiview,Iphone,Objective C,Xcode,Cocoa Touch,Uiview,我有3个UIView图像。(我用故事板画了它们) 我并不总是显示3个UIM图像。我像这样隐藏和显示它们: facebookIcon.hidden = YES/NO; 我想显示它们之间的精确间距 例如,如果有一个图像-它将位于中心 如果有两个图像-将有一个中心的每一边 如果有3张图片-一张在右边,一张在中间,一张在左边 与UITabViewController图标类似,这些图标会以精确的空格自动展开 我该怎么做呢?简而言之:在iOS 6中使用 或自动调整iOS 6之前版本的大小 // auto

我有3个UIView图像。(我用故事板画了它们)

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

facebookIcon.hidden = YES/NO;
我想显示它们之间的精确间距

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

如果有两个图像-将有一个中心的每一边

如果有3张图片-一张在右边,一张在中间,一张在左边

UITabViewController
图标类似,这些图标会以精确的空格自动展开

我该怎么做呢?

简而言之:在iOS 6中使用

或自动调整iOS 6之前版本的大小

// auto resizing masks
self.button1.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.button2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.button3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
请注意,您可以在Interface Builder中完成所有自动布局/调整大小的遮罩。

简而言之:在iOS 6中使用

或自动调整iOS 6之前版本的大小

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

请注意,您可以在Interface Builder中执行所有自动布局/调整掩码。

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

    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;
    }
CGFloat padding=10//你的愿望
NSMutableArray*notHidden=[NSMutableArray];
用于(UIImageView*img在self.view.subview中){
如果(!img.hidden){
[notHidden addObject:img];
}
}
int arrayCount=notHidden.count;
交换机(阵列计数%2){
案例0:{
for(int index=-arrayCount/2;index=0){
tempIndex=index+1;
}
CGFloat xDiff=img.frame.size.width*索引+padding*临时索引;
tempFrame.origin.x=self.view.center.x+xDiff;
[[notHidden objectAtIndex:index+arrayCount/2]setFrame:tempFrame];
}
打破
}
案例1:{

对于(int index=-arrayCount/2;index而不使用自动布局或
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;
    }
CGFloat padding=10;//您的愿望
NSMutableArray*notHidden=[NSMutableArray];
用于(UIImageView*img在self.view.subview中){
如果(!img.hidden){
[notHidden addObject:img];
}
}
int arrayCount=notHidden.count;
交换机(阵列计数%2){
案例0:{
for(int index=-arrayCount/2;index=0){
tempIndex=index+1;
}
CGFloat xDiff=img.frame.size.width*索引+padding*临时索引;
tempFrame.origin.x=self.view.center.x+xDiff;
[[notHidden objectAtIndex:index+arrayCount/2]setFrame:tempFrame];
}
打破
}
案例1:{

对于(int index=-arrayCount/2;index而言,解决方案如下:

- (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];
    }
}

解决办法如下:

- (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];
    }
}


您是否希望将调整大小设置为动画?否。显示时为静态您希望不使用自动布局执行此操作?我希望仅在特定的UIView上执行此操作这些UIImageView大小相同吗?是否希望将调整大小设置为动画?否。显示时为静态您希望不使用自动布局执行此操作?我希望如此这些UIImageView是否大小相同?我使用的是io6,但自动布局适用于整个故事板,而不是UIViewControlleri中的一个视图如果启用了自动布局,则可以对任何一组视图使用约束。但是上面的代码不会阻止按钮或图像重叠。我不太熟悉你可以在这里看到:我使用的是io6,但是自动布局用于整个故事板,而不是UIViewControlleri中的一个视图。如果启用了自动布局,你可以对任何一组视图使用约束。但是上面的代码不会阻止按钮或图像重叠。我对它不是很熟悉。你可以在这里看到:自动布局的问题代码太具体了。如果我有4个UIImages怎么办?写代码是因为你的问题特别说明了3个图像。如果我可以改变它以容纳任何数字,请告诉我c。图像的数量不应该是static@Odelya,如果您正在考虑可伸缩性,则必须决定是使用AutoResizingMask还是AutoLayoutded,为变量计算最小/最大情况(在本例中为图像数)。还有一种情况是,超过一定数量的图像,您需要一个滚动视图来显示更多的内容。做出这些决定后,您或“任何给出答案的人”将节省大量时间回答问题。@Calvinbai我希望使用自动重设网格而不是自动布局。代码的问题是它太具体了。如果我会有4个UIImages吗?编写代码是因为你的问题特别说明了3个图像。如果我可以更改它以容纳任何数量的图像,请告诉我c。图像的数量不应该太多static@Odelya,如果您考虑可伸缩性,则必须决定是使用AutoResizingMask还是AutoLayout可编辑(在本例中为图像数量)。还有一种情况是,如果图像数量超过一定数量,则需要滚动视图来显示更多内容。做出这些决定后,您或“给出答案的任何人”将节省大量时间回答问题。@Calvinbai我希望使用AutoResizingMask,而不是autolayout。