A reversible color change of a polyaniline-infiltrated TiO2 inverse opal photonic crystal (PC) film can be obtained when the PC is switched from an acidic to alkali vapor environment. In a saturated NH3 environment, the stopband of the as-prepared PCs changes from 556 to 688 nm; such large shift of 132 nm could be observed, corresponding to a clear color change from green to red. After placing in HCl vapor, the stopband undergoes a blue-shift and the color turns back to green. The result is ascribed to PANI being doped or dedoped by acid or base and the effective refractive index of the PC film varying accordingly. The naked-eye detection of NH3 and HCl vapors can be realized by the reversible color change of the PC film, which is of importance for chemical and biological sensors.