And to Yosef two sons were born, before the years of hunger arrived...
The Gemara deduces from this possuk that a person must separate from his wife during times of hunger. It would be unseemly for a person to live with his wife as normal when the world is suffering from hunger.
The Ohr Hachaim explains that the Gemara does not mean to deduce this from the fact that Yosef’s children were born prior to the start of the hunger years. This explanation is untenable, because Yosef was not bound by this law. This prohibition only applies to someone who has already discharged his obligation of giving birth to children, and Yosef had not done so yet. Yosef would have been permitted to live with his wife even after the years of hunger had begun.
However, seeing as the possuk deems it important to clarify that the children were born prior to the start of the seven hunger years, it seems to be telling us that there was a difference between the years as regards birthing children. Although this difference did not pertain to Yosef, it did to everyone else.
The Ohr Hachaim explains that this answers the question that Tosfos brings up. Tosfos asks about Yocheved, daughter of Levi. Chazal tell us that she was born to Levi as they entered Mitzrayim. The world was still in the throes of the hunger years, and Levi too should have been prohibited from living with his wife. How could he have had a daughter when the world was suffering hunger?
Tosfos answers his question with the novel idea that this prohibition against continuing familial relations during a hunger is merely a מדת חסידות – a pious act. A person is not actually prohibited from living with his wife, but it is correct for a person to refrain from marital relations at that time, and a pious person will act so. Levi did not act as a pious person, he merely discharged his basic obligations.
The Ohr Hachaim refuses to accept this explanation, claiming that the possuk itself does not allow him to do so. The Torah describes Levi as איש חסידיך – the extra pious man. Levi would certainly have acted with this extra piety and separated from his wife during the years of hunger.
The true reason that Levi was permitted to birth a child during the hunger years is that he, as Yosef, had not yet discharged his obligation to have children. Although he had given birth to three sons, he still did not have a daughter. The Halacha is that a person must ensure to birth at least a son and daughter, and until Levi had done so, he could not separate from his wife.
Another explanation offered by the Ohr Hachaim is that this prohibition against a person having marital relations with his wife during times of hunger only applies when the hunger affects Am Yisroel. When it only affects the surrounding nations, nothing is prohibited.
Chazal tell us that Yaakov did not need to send his children to buy produce, as he had enough. He only sent them to Mitzrayim in order that the people around him should not consider him an outlier and a target for jealousy.
Although this was only true in the first year of the hunger, and afterward, even Yaakov’s stores dwindled, this did not cause a problem for Levi. Levi was permitted to live with his wife during the first year, and she could have stayed in her mother’s womb for the entire second year.
However, this would not have permitted Yosef to live with his wife during the hunger years. Yosef did not know the situation with his father and brothers, and he could not be sure that they had enough to eat. He needed to separate from his wife, and that is why the Torah tells us that his children were born prior to the start of the hunger years.