WASHINGTON — The White House’s directive to hide a Navy destroyer named after Senator John McCain during President Trump’s recent visit to a naval base in Japan was driven, administration officials said on Thursday, by a fear of bad visuals — the name of the president’s nemesis clearly visible in photographs of him.

In truth, it would have been a bad visual for only one person: Mr. Trump.

Yet an effort to airbrush an American warship by covering its name with a giant tarp and then hiding it with a barge demonstrates how anxious the Trump administration has become about the grudges of the president. It also shows the extraordinary lengths officials in the bureaucracy are willing to go to avoid provoking Mr. Trump.

Sailors from the McCain were not invited to Mr. Trump’s speech on another ship, the Wasp, at the Yokosuka Naval Base, although crew members from most other American ships at the base were, a Navy service member based at Yokosuka said.

When several sailors from the McCain — wearing uniforms that bore the ship’s name and insignia — turned up anyway at the Wasp to hear Mr. Trump’s speech, they were turned away, the service member said. The service member, who requested anonymity because he was not allowed to speak publicly, said that a gate guard told the two sailors they were not allowed on the Wasp because they were from the McCain.