选择 Xcode 6 Beta 时,UITabBarItem 图像消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25272394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITabBarItem Image disappearing when selected Xcode 6 Beta
提问by deltajuliet
Fairly new to development however the Xcode Betas are giving me a bit of a headache (Xcode 6 beta 5 / iOS 8).
相当新的开发但是 Xcode Betas 让我有点头疼(Xcode 6 beta 5 / iOS 8)。
Have found some answers but mostly relating to Xcode 5 and under.
找到了一些答案,但主要与 Xcode 5 及以下版本有关。
Everything runs fine, however the selected icon/view controller disappears. In Xcode 5-4 the image assets weren't even coming through giving the following error message. Now they do, however the error message still runs.
一切运行正常,但是选定的图标/视图控制器消失了。在 Xcode 5-4 中,图像资产甚至没有通过给出以下错误消息。现在他们这样做了,但是错误消息仍然运行。
Screenshot:
截屏:
CUICatalog: Invalid asset name supplied: (null) 2014-08-12 15:16:26.521 TheApp[5275:3231837] CUICatalog: Invalid asset name supplied: (null) 2014-08-12 15:16:26.521 TheApp[5275:3231837] Could not load the "(null)" image referenced from a nib in the bundle with identifier "YourCompnay.TheApp"
CUICatalog:提供的资产名称无效:(空)2014-08-12 15:16:26.521 TheApp[5275:3231837] CUICatalog:提供的资产名称无效:(空)2014-08-12 15:16:26.5275:TheApp[5275:3231837] 3231837] 无法加载从标识符为“YourCompnay.TheApp”的包中的笔尖引用的“(空)”图像
I'm racking my brain trying to figure out if it's my connections / images or just an Xcode beta bug.
我绞尽脑汁想弄清楚这是我的连接/图像还是只是 Xcode beta 错误。
Def need help and a clear explanation. ELI5.
Def 需要帮助和明确的解释。ELI5.
Any help would be appreciated. I can't seem to find anything specifically relating to this error.
任何帮助,将不胜感激。我似乎找不到与此错误相关的任何内容。
回答by Yjo-jo
I am in almost the exact same spot as you. Like you, my "unselected" tab images would show up fine, but for my selected tab images, they would not be present, and I'd get the "could not load null image..." error.
我和你几乎完全一样的地方。像您一样,我的“未选择”标签图像会正常显示,但对于我选择的标签图像,它们将不存在,并且我会收到“无法加载空图像...”错误。
I had been assigning the images within the Storyboard IB... perhaps this is what you were doing too. Either way, within IB in the Attributes Inspector for the UITabBarItem, there are two different fields. One is under the Tab Bar Item section and it is named Selected Image. The other is under the Bar Item section and it is named Image. Just to be clear (restating my first paragraph but in terms of IB), I had "valid" images that I had saved within my .xcassets assigned to each of these Image/Selected_Image fields (each image was slightly different from the other to distinguish selection). The one that I had in my Image field would show up, and the one that I had in my Selected Image field would not.
我一直在 Storyboard IB 中分配图像......也许这也是你正在做的。无论哪种方式,在 UITabBarItem 的 Attributes Inspector 中的 IB 中,都有两个不同的字段。一个位于 Tab Bar Item 部分下,它被命名为 Selected Image。另一个位于 Bar Item 部分下,名为 Image。为了清楚起见(重申我的第一段,但就 IB 而言),我在分配给每个 Image/Selected_Image 字段的 .xcassets 中保存了“有效”图像(每个图像与其他图像略有不同,以区分选择)。我在 Image 字段中的那个会出现,而我在 Selected Image 字段中的那个不会出现。
If I put the "good" one from the Image field into the Selected Image field, I would still get the error, which didn't make sense to me.
如果我将 Image 字段中的“好”一个放入 Selected Image 字段,我仍然会收到错误,这对我来说没有意义。
HOWEVER, if I only put my "unselected" image into the Bar Item -> Image field and left the Tab Bar Item -> Selected Image empty, THEN the one image would get used in both places AND it would be highlighted blue when selected. This didn't give me the minor image change that I wanted (as my review of the Apple Docs for Bar Button Itemindicated I should have "thicker lines" in my image for the selected item), it does at least give me an image for the selected tab. Hopefully this will help you as well.
但是,如果我只将我的“未选择”图像放入 Bar Item -> Image 字段并将 Tab Bar Item -> Selected Image 留空,那么这一张图像将在两个地方都使用,并且在选择时会以蓝色突出显示。这并没有给我想要的小图像更改(因为我对Apple Docs for Bar Button Item 的评论表明我应该在所选项目的图像中使用“更粗的线条”),它至少给了我一个图像对于选定的选项卡。希望这也能帮助你。
回答by ttarik
Yjo-jo's answer is not actually a solution to this problem. It makes the tab bar use the unselected image for both normal and selected states, but does not let you use a different image for the selected state, as recommended in Apple's docs.
Yjo-jo 的回答实际上并不是这个问题的解决方案。它使标签栏对正常和选定状态使用未选定的图像,但不允许您为选定状态使用不同的图像,如 Apple 文档中所建议的那样。
The bug is that the 'selected image' field for tab bar items in storyboards simply does not work, and it is still a problem as of Xcode 6.0.1 - I'm using images directly included in my app's resources rather than .xcassets and I'm experiencing the same issue. The images exist in my app's bundle, they appear in the drop-down box when editing my storyboard, and yet I get the same Could not load the "(null)" image ...
error, and a blank selected image.
该错误是故事板中标签栏项目的“选定图像”字段根本不起作用,并且从 Xcode 6.0.1 开始它仍然是一个问题 - 我使用的是直接包含在我的应用程序资源中的图像而不是 .xcassets 和我遇到了同样的问题。这些图像存在于我的应用程序包中,它们在编辑我的故事板时出现在下拉框中,但我遇到了同样的Could not load the "(null)" image ...
错误,并且选择了一个空白的图像。
My solution was to add this in my tab bar item's (view controller) viewWillAppear
method:
我的解决方案是将其添加到我的标签栏项目(视图控制器)viewWillAppear
方法中:
[[self tabBarItem] setSelectedImage:[UIImage imageNamed:@"(selected image file)"]];
However, when doing this, I found that the size of the image changed ever so slightly when being selected/unselected, although my images were the same size. This might be because the size of the images for tab bar items is precalculated in some specific way, and we're messing around with them just as they're about to be drawn. It's a small issue but it was enough to bug me, so in order to get them perfect I removed the previous code and added this in my tab bar controller'sviewWillAppear
method:
但是,在执行此操作时,我发现在选择/取消选择时图像的大小发生了微小的变化,尽管我的图像大小相同。这可能是因为标签栏项目的图像大小是以某种特定方式预先计算的,而我们在它们即将被绘制时正在处理它们。这是一个小问题,但足以让我烦恼,所以为了让它们完美无缺,我删除了以前的代码并将其添加到我的标签栏控制器的viewWillAppear
方法中:
[(UITabBarItem*)[[[self tabBar] items] objectAtIndex:n] setImage:[UIImage imageNamed:@"(unselected image file)"]];
[(UITabBarItem*)[[[self tabBar] items] objectAtIndex:n] setSelectedImage:[UIImage imageNamed:@"(selected image file)"]];
Where n
is the index of the tab bar item. (starting from 0)
n
标签栏项的索引在哪里。(从0开始)
It's ugly but it works. As far as I know, the "selected image" field just blatantly does not work in Xcode 6.
这是丑陋的,但它的工作原理。据我所知,“选定图像”字段在 Xcode 6 中显然不起作用。
回答by Pierre Houston
Defining selectedImage
in User Defined Runtime Attributes like in this answerworked for me.
selectedImage
在这个答案中定义用户定义的运行时属性对我有用。
回答by totocaster
Looks like a bug in UIKit, I've filed a radaralready.
看起来像 UIKit 中的一个错误,我已经提交了一个雷达。
In order to make this work however, you should add icons manually from your view controller code. There are few things to do:
但是,为了使这项工作正常进行,您应该从视图控制器代码中手动添加图标。有几件事要做:
- Detect which
UITabBarItem
belongs to your view controller
- 检测哪个
UITabBarItem
属于您的视图控制器
YourViewController.m:
你的ViewController.m:
-(UITabBarItem *)tabBarItem {
return self.tabBarController.tabBar.items[[self.tabBarController.viewControllers indexOfObject:self.navigationController]];
}
Note self.navigationController
, you should just self
if there is no navigation view controller wrapping your regular view controller.
请注意self.navigationController
,self
如果没有导航视图控制器包裹您的常规视图控制器,您应该这样做。
- Change icons
- 更改图标
YourViewController.m-awakeFromNib:
function:
YourViewController.m-awakeFromNib:
函数:
[self.tabBarItem setImage:[UIImage imageNamed:@"RegularImage"]];
[self.tabBarItem setSelectedImage:[UIImage imageNamed:@"SelectedImage"]];
Works for me, should work for you too. It's a bit of a hack, but as elegant as it can be.
对我有用,也应该对你有用。这有点像黑客,但尽可能优雅。
回答by Ameya Vaidya
Click the TabButtonItem and go to the Attributes inspector. You will see 2 sections: Tab Bar Item & Bar Item. You probably filled in the selected image field under the Tab Bar Item and the Image field under the Bar Item. You need to keep the Tab Bar Item: Selected Imagefield empty. It will work then.
单击 TabButtonItem 并转到属性检查器。您将看到 2 个部分:标签栏项目和栏项目。您可能填写了 Tab Bar Item 下的选定图像字段和 Bar Item 下的 Image 字段。您需要将Tab Bar Item: Selected Image字段保持为空。那时它会起作用。
回答by Steve N
I have been seeing this issue across several versions of XCode. Now on 6.3 beta 3, I finally fixed it.
我一直在多个版本的 XCode 中看到这个问题。现在在 6.3 beta 3 上,我终于修复了它。
In my case, I solved this by doing two things:
就我而言,我通过做两件事解决了这个问题:
- For each tab bar item, in Interface Builder, remove the "Selected Image" field (this may have been unnecessary).
- 对于每个选项卡栏项目,在 Interface Builder 中,删除“Selected Image”字段(这可能是不必要的)。
- Right-click on your storyboard and select "Open As -> Source Code".. search for "tabBarItem". Review each item. They should look something like this:
- 右键单击您的故事板并选择“打开为 -> 源代码”.. 搜索“tabBarItem”。查看每个项目。它们应该如下所示:
In my case, there were duplicate/orphaned tabBarItem elements that appeared out of place and with bad/old settings. Comment out any elements that do not match your settings in IB. Clean & rebuild your app and see if you're fixed like I was.
就我而言,存在重复/孤立的 tabBarItem 元素,这些元素看起来不合时宜且设置错误/旧。注释掉与您在 IB 中的设置不匹配的任何元素。清理并重建您的应用程序,看看您是否像我一样修复了。
回答by NSGodMode
Here's the solution, This worked very well for me.
这是解决方案,这对我来说非常有效。
- Select the tabBarItem and set system item: custom and selected Image: empty in attribute inspector
- Go to Identity inspector and remove the keypath that indicates any image
- 选择 tabBarItem 并设置系统项:自定义和选定的图像:属性检查器中的空
- 转到身份检查器并删除指示任何图像的键路径
回答by Terry Bu
For anybody else suffering from the same annoying errors: "CUICatalog: Invalid asset name supplied: (null) 2014-08-12 15:16:26.521 TheApp[5275:3231837] CUICatalog: Invalid asset name supplied: (null) 2014-08-12 15:16:26.521 TheApp[5275:3231837] Could not load the "(null)" image referenced from a nib in the bundle" ... Try selecting "Image" from the "Bar Item" section of the Attributes Inspector to set the image. I'm not exactly sure what the difference is but the "Selected Image" dropdown of the "Tab Bar Item" section throws this error and doesn't work.
对于遭受同样烦人错误的其他人:“CUICatalog:提供的资产名称无效:(空)2014-08-12 15:16:26.521 TheApp[5275:3231837] CUICatalog:提供的资产名称无效:(空)2014-08 -12 15:16:26.521 TheApp[5275:3231837] 无法加载从包中笔尖引用的“(空)”图像”......尝试从属性检查器的“条形项目”部分选择“图像”设置图像。我不确定有什么区别,但“选项卡栏项目”部分的“选定图像”下拉列表会引发此错误并且不起作用。