The UN argument boils down to Spain lobbying the UN early on (post-WW2) at a time when Morocco and other North African and West Asian states either did not exist yet or did not have the ability to represent themselves well at the UN.
Gibraltar, on the other hand, was just lost in the War of Spanish succession to the British Empire, and has had a British connection to them since then, but never before
But applying this logic universally would change the world map, not to mention that this justification ignores the fact that the concept of a nation state is relatively new as a norm.
Regardless, I and many others will continue to ridicule Spain’s paradoxical position on Gibraltar. Hopefully a just resolution is realized for all parties at some point in the future.
As an aside, I have always found it ironic how Spain continuously whines about Gibraltar while doing the exact same thing to Morocco. At the very least, Spain was smart/lucky enough to lobby the UN early on to not include Ceuta and Melilla in the decolonization list.