【问题标题】:iOS 7.1 issue - Tabbar icon image is automatically resize when touch and drag on that tab buttoniOS 7.1 问题 - 触摸并拖动该选项卡按钮时,Tabbar 图标图像会自动调整大小
【发布时间】:2014-04-17 14:11:20
【问题描述】:

我有这个代码

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"tab_pressed_home_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_home_icon"]];

tabBarItem1.imageInsets = UIEdgeInsetsMake(8, 0, -2, 0);

在标签栏上设置一个图标。

到目前为止一切正常,直到昨晚我更新 Xcode 5.1

并在 ios7.1 模拟器上运行应用程序。

这是应用程序

现在,当我点击标签栏时,图标图像的大小会减小,而当我松开手指时,图像会恢复正常。 但是如果我点击图标并拖动它,图像看起来像这样(按比例缩小)。

像这样

怎么会这样? 有没有办法解决这个问题?

谢谢。

【问题讨论】:

  • 我遇到了同样的问题,发现它是由使用 UITabBarItem 设置图像插入引起的。这是代码:[barItem setImageInsets:UIEdgeInsetsMake(15, 7.5, 0, 7.5)];在评论时一切顺利。
  • 是的,它在 IOS 7.1 中造成了问题。而且我认为现在标签栏高度已更改为 44。
  • 这里也一样。这个bug真恶心!奇怪的是,它只发生在我的一个标签栏图标上,我什至不使用情节提要......
  • 你试过在设备上运行它吗?我在简单的 uiimageView 上遇到了类似的问题 - 它在设备上看起来不错,但在模拟器上拉伸。
  • 有没有人找到解决问题的办法?

标签: ios uitabbarcontroller uitabbaritem


【解决方案1】:

正如其他人提到的那样,通过设置 tabBarItem 的 imageInsets 解决了这个问题。您可以在代码中执行此操作,也可以在 Interface Builder 中执行此操作,没关系。

重要的是让顶部插图与底部插图相等。

【讨论】:

  • 这解决了问题。基本上他们必须互相抵消:item.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
  • 顶部插图不必等于底部插图。上下之和应等于左右之和(如果您不想拉伸或收缩)。
  • @clocksmith 我的都是平等的,我仍然有这个问题
【解决方案2】:

我在 iOS 7.1 上尝试通过以下代码设置图像插入时遇到了同样的问题:

[self.tabBarItem setImageInsets:UIEdgeInsetsMake(5, 0, -5, 0)];

所以我直接使用 Storyboard 上的 Bar Item Size 解决了这个问题。

考虑到要使其正常工作,您应该按以下方式分配 TabBarItem 的图像

UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *myItem = [tabBar.items objectAtIndex:0];

[homeItem setFinishedSelectedImage:[UIImage imageNamed:@"A.png"]
       withFinishedUnselectedImage:[UIImage imageNamed:@"B.png"]];

不是这样

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"A.png"]
              withFinishedUnselectedImage:[UIImage imageNamed:@"B.png"]];

更新

要访问 Bar Item Size,请直接选择 Tab Bar Controller 的任何子项的 Scene 下的“Item”元素。 (图一)

【讨论】:

  • 如何到达故事板的bar item size区域?
  • 直接选择你的标签栏控制器的孩子,他们有一个代表栏项目的“项目”。我将上传一张图片并更新我的答案
【解决方案3】:

我的问题也是类似的,7.1更新时tabbar图标的位置发生了变化,这个问题真的很烦人,我做了一个快速的解决方案,因为我必须批准该应用程序。这是:

好的,我不确定这是最好的解决方案,但对我来说可行。

【讨论】:

    【解决方案4】:

    同样的问题。同样在更新到 iOS 7.1 和 xcode 5.1 之后,我的解决方案:标签栏项目的大小设置为底部的 4。(在大小检查器中)我像所有其他人一样将其更改为 0,问题就消失了。

    【讨论】:

      【解决方案5】:

      我放弃了,我的解决办法在这里:

      使用UIImage函数调整大小和偏移

      - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize andOffSet:(CGPoint)offSet{
          UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
          [image drawInRect:CGRectMake(offSet.x, offSet.y, newSize.width-offSet.x, newSize.height-offSet.y)];
          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
      }
      

      并正确设置标签栏项目图像:

      UITabBarItem *barItem  = [self.tabBarController.tabBar.items objectAtIndex:i];
      CGSize size = CGSizeMake(49, 49);
      CGPoint offset = CGPointMake(10, 10);
      [barItem setFinishedSelectedImage:[self imageWithImage:selectedImage scaledToSize:size andOffSet:offset]
               withFinishedUnselectedImage:[self imageWithImage:unSelectedImage scaledToSize:size andOffSet:offset]];
      

      如果有任何其他解决方案会有所帮助!

      【讨论】:

      • setFinishedSelectedImage 在 iOS 7.0 中已弃用
      【解决方案6】:

      我的答案基于此处发布的其他人 - 将标签栏项目中的所有 ImageInsets 设置为 0:

      for (UITabBarItem* item in self.tabBar.items)
      {
          [item setImageInsets: UIEdgeInsetsMake(0, 0, 0, 0)];
      }
      

      【讨论】:

        【解决方案7】:

        您可以在标签栏顶部放置一个 UIView,并在 UIView 中添加一个 UITapGestureReognizer。点击时,我确定它在哪里,并为标签栏调用适当的选定项目。 Hack,但效果很好,可以让您将插入值保持为您想要的任何值。

         @property UIView  tabBarCover;//place on top of uitabbar and give it clear color background
        
         - (void)viewDidLoad
         {
           [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
            UITapGestureRecognizer *singleFingerTap =
                                [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handleSingleTap:)];
           [tabBarCover addGestureRecognizer:singleFingerTap];
        
         }
        

        然后每次点击 UIView 时,根据用户触摸的位置设置选定项。我有 3 个标签栏项目,所以我只是为 x 坐标做了一些逻辑。

        -(void) handleSingleTap:(UITapGestureRecognizer *) recognizer
        {
            CGPoint location = [recognizer locationInView:[recognizer.view superview]];
            //NSLog(@"tapped it %lf", location.x);
            if(location.x<=105){
                //1st 3rd tapped, do something
        
            }
            else if(location.x >105 && location.x<=210)
            {
               //do nothing, selected item stays same. this is glas
            }else{
               //must be in 3rd section so do that load
        
            }
        
        }
        

        希望对你有帮助。

        【讨论】:

          【解决方案8】:

          我花了大约 3 个小时!我发现了我们的错误。这不是 iOS 的 bug。

          注意:imageInsets 无济于事!因为这是图像比例的问题。我玩了2个小时!没有结果。尝试解决问题的方法很愚蠢!

          请看这个项目http://www.raywenderlich.com/50310/storyboards-tutorial-in-ios-7-part-2

          在这个项目中,玩家和手势图标完美运行!因为有视网膜和非视网膜的图标。

          如果你有两个比例的图标,那么没问题。使用它们!一切都会好起来的!它是 1 变体!

          2 变体。如果您只有一张大图,并且想同时用于两个显示,那么您需要指定调整后图像的“比例”:

          (UIImage *)thumbnailImageSize:(CGSize)size fromImage:(UIImage *)image { 
              if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 3.00) {
                  _scale = 3.00; 
              } else if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) { 
                  _scale = 2.00; 
              } else 
                  _scale = 1.00;
          
              UIGraphicsBeginImageContextWithOptions(size, NO, _scale); 
              [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
              UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext(); 
              return newImage; 
          }
          

          【讨论】:

            【解决方案9】:

            我的解决方案是将 x 和 y 点除以 2,然后将其分别设置为左、右、上和下。

            【讨论】:

              【解决方案10】:

              试试这个应该可以解决问题并且不会影响旧版本

              将以下几行放入任何控制器的 init 方法中:

              UIImage* image = [UIImage imageNamed: @"Your tabbar image name goes here"];
                  if ([image respondsToSelector: @selector(imageWithRenderingMode:)])
                          [self.tabBarItem setImage: [image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
              

              【讨论】:

              • 我认为我在这里犯了一个错误,根据您的问题,您的问题在于所选图像,因此在代码的最后一行将“setImage:”替换为“setSelectedImage:”。
              【解决方案11】:

              顶部和底部插图在您的代码中设置 - 只需设置其中的一个(例如顶部)就足够了。突出显示的图像被压缩的原因可能是光栅具有不同的尺寸,与默认图像不同——在这种情况下,插图使图像垂直缩放。

              【讨论】:

                【解决方案12】:

                我通过对 UIImage 使用从 UIView 创建图像的这个类别解决了同样的问题

                + (UIImage *) imageWithView:(UIView *)view
                {
                    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
                    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
                
                    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
                
                    UIGraphicsEndImageContext();
                
                    return img;
                }
                

                然后,使用此方法,您可以使用所需的自定义偏移量创建新图像。 (我只需要 Y 偏移)。

                -(UIImage *) uiNewImageForTabBar: (UIImage *) i withOffsetForY: (CGFloat) offsetY
                {
                    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, i.size.width, i.size.height+offsetY)];
                    [v setBackgroundColor:[UIColor clearColor]];
                    [v setOpaque:NO];
                
                    UIImageView *sub = [[UIImageView alloc] initWithImage:i];
                    [sub setY:offsetY];
                
                    [v addSubview:sub];
                
                    return [UIImage imageWithView:v];
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2014-05-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-06-19
                  • 1970-01-01
                  • 2014-11-05
                  • 2019-09-25
                  • 1970-01-01
                  相关资源
                  最近更新 更多