The hive swarmed because you possibly did not add space as they grew.
Strong hives will use 4-5 full supers; 3 will be for excess honey storage.
One should place a retainer above the second full super to allow the workers in but keep the queen out.
As super #4 starts to fill with honey, #3 can be removed and an empty super placed on the stack.
Note: An empty super is one with all the frames having a wax insert but not cells. the bees will follow the pattern of the insert to build up the cells and deposit the honey inside.
I expanded the hive 2 days before they swarmed. They made up their mind before I added the super.
Not knowing anything, I thought they would only swarm if there were two queens in the hive (since I had just expanded). So I then went through looking for a queen, thought I might have seen one, and killed every queen cell I saw. Needless to say, I was without a queen for 3 weeks. I had to take a frame out of my buddy's hive that had fresh eggs and put it into mine. Luckily, I still had enough worker bees left to make another queen.
My hive looked pretty sparse there for a while. I felt lucky to get as much honey as I did at the end of the season.