简体   繁体   中英

UITableViewController's background color set to default (white), but appears black

In my storyboard, I set my table view controller's background color to the "default" setting, which shows up as white in interface builder. However, when I run the app, the table view controller's background color is black.

在此处输入图片说明

This seems to be a bug in Apple's code.

Steps to reproduce:

  1. Create a brand new Xcode project.
  2. Drag and drop a table view controller onto the storyboard, and make it the initial view controller.
  3. Select the table view inside the table view controller and change its background color from "[white] Default" to "[white] Default" (ie reselect the top option in the list).
  4. Run the app.
  5. Even though Interface Builder showed the background color as being "[white] Default", the actual background color at runtime is black. It should clearly be white, as that is the default color Interface Builder reported.

If you look at the xml file, you see something interesting. Dropping a brand new table view controller onto a storyboard will show the "[white] Default" option selected in the storyboard, however the xml will have a background color entry: <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> . This is peculiar because usually when something is set to its default value, it will be excluded from the xml file.

In Interface Builder, if you reselect the "[white] Default" option again, you will notice that the background color behind transparent views (eg the section header) will appear to change slightly. This is because even though the "[white] Default" option is shown as being selected, it has actually removed the <color> entry in the xml file. Normally, this should select the default background color for the view. In this case, it chooses black. The Interface Builder UI is now no longer in sync with the xml file.

In Interface Builder, if you now select the "[white] White Color" option, the interface will show "[white] Default", and everything will be back to how it was when you originally dropped the table view controller on the storyboard.

This means that Interface Builder shows "[white] Default" for both when black and white are the selected options. The only way to know which is selected is at runtime, or by paying attention to subtle differences to transparent views in Interface Builder.

Therefore, in order to solve this problem, don't choose the "[white] Default" option in the background color setting, choose the "[white] White Color" option.

To summarize

Selecting this:

在此处输入图片说明

... will show this:

在此处输入图片说明

... but the table view will appear like this: (buggy behavior)

在此处输入图片说明

On the other hand, selecting this:

在此处输入图片说明

... will show this:

在此处输入图片说明

... and the table view will appear like this: (correct behavior)

在此处输入图片说明

For changing the background color try to nil out the backgroundView property and then set the backgroundColor on the desired table view.

Try to use this:

  [self.tableView setBackgroundView:nil];
  [self.tableView setBackgroundColor:[UIColor whiteColor]];

Hope this can help you.

我在xcode-7-beta中遇到了同样的问题,发现的唯一“解决方法”是将TableView样式从Plain切换为Grouped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM