Did Mahatma Gandhi ever publicly declare his opinion on the racist segregation under the apartheid regime in South Africa?
No, because apartheid wasn't introduced until May 1948, whereas Gandhi died in January 1948.
There was, of course, racism in South-Africa long before that; including in the laws of the country. But the apartheid policies expanded a great deal on that.
What you're probably aiming at with this question are Gandhi's views on black people, which are not particularly flattering by today's standards; while living in South-Africa from 1893 to 1914 he called black people "savages" and implied they were inferior to white people on several occasions. For example in 1893 Gandhi wrote that a "general belief seems to prevail in the Colony that the Indians are a little better, if at all, than savages or the Natives of Africa". There are quite a few more examples of a similar nature, including some from the late '30s.
Ironically, at the same time he was fighting for the rights of Indian people in South Africa.
Whether he was truly "racist" or just a "man of his time" is a rather controversial topic, and not something that be easily settled in an answer here (or anywhere else, for that matter), so you'll have to decide that for yourself :-)
Mahatma Gandhi spent a large amount of time and energy directly fighting apartheid in South Africa. The thing is, apartheid, while we think about it as focused entirely against those who were considered "black," was really a system of formalized oppression against all non-whites. Gandhi's energies were often focused measure that targeted those of Indian descent.
Early in his life, he took an intentional non-political stance to try and help his career as an attorney. He was discriminated against on a train in South Africa, where he had traveled for employment.
In 1893 he arrived in Durban where he remained for a week before leaving for Pretoria by train. He purchased a first-class ticket, boarded the train and started work on his lawsuit. During the journey a white woman passenger complained about sharing a compartment with a "coolie" and Gandhi was asked to move to a third-class carriage. On his refusal he was forcibly removed from the train at Pitermaritzburgh Station. Here he spent the night and later he described the event as the most prominent influence on his political career.
So, clearly, it's false that he never made a statement. He stated that his first experience with apartheid was THE seminal moment in his political life. He also spent a lot of time both in India and in South Africa organizing protests against apartheid policies, albeit, mostly ones targeting those of Indian descent.
The claim that he was silent or took no stand on apartheid appears to be false.
Ghandi was fighting for Indians, not Blacks and he was in fact heavily in favor of separation(apartheid) between Indians and Blacks(who he thought of as savages). E.g.
In the Indian Opinion of March 25, 1905, Gandhi wrote on a Bill regulating fire-arms: "In the instance of fire-arms, the Asiatic has been most improperly bracketed with the natives. The British Indian does not need any such restrictions as are imposed by the Bill on the natives regarding the carrying of fire-arms. The prominent race can remain so by preventing the native from arming himself. Is there the slightest vestige of justification for so preventing the British Indians?" Gandhi always advised Indians not to align with other political groups in either colored or African communities. He was strongly opposed to the commingling of races.
One of the first battles Gandhi fought after coming to South Africa was over the separate entrances for whites and blacks at the Durban post office. Gandhi objected that Indians were “classed with the natives of South Africa,” who he called the kaffirs, and demanded a separate entrance for Indians.
“We felt the indignity too much and … petitioned the authorities to do away with the invidious distinction, and they have now provided three separate entrances for natives, Asiatics and Europeans.”
In a petition letter in 1895, Gandhi also expressed concern that a lower legal standing for Indians would result in degenerating "so much so that from their civilised habits, they would be degraded to the habits of the aboriginal Natives, and a generation hence, between the progeny of the Indians and the Natives, there will be very little difference in habits, and customs and thought."
and it's not like he changed his views back in India
In 1939, Gandhi justified his counsel to the Indian community in South Africa against forming a non-European front: “I have no doubt about the soundness of my advice. However much one may sympathise with the Bantus, Indians cannot make common cause with them.”