Look at a version of the bag mod where each slot had an actual onshow event that determined sizing of the frame and the overall bag. Use the mod by
ger or My
depechenode's Advanced 3/4 Bag w/titles (which I used ger's mod for MY bag as well, I just changed a few things to my liking). Either one will show you how to use the sizing part.
Basically each slot will look to see if it is being shown if so resize the bag frame to fit. If not, resize ridding the dead space. But all is tied to onshow events.