Though the courts have divided over how to resolve dress-code disputes and have reached different results, there has not been a successful challenge to a hat regulation in public schools. So a prohibition on wearing hats in school would probably be upheld.
School boards have used a variety of reasons to support policies on uniforms and dress codes. They say standard attire helps lessen peer pressure aggravated by socioeconomic divisions, promote unity of spirit, identify trespassers on school grounds and prevent gang-related violence. These concerns would likely be among the reasons for prohibiting hats in schools, and such rationales have been upheld in many courts.
The one exception schools would probably make would be for religious headwear.