What you can do is check the global position of the UI element. Using the X/Y values:
0, 0 is the middle of the screen
-1, -1 is the bottom left of the screen
1, 1 is the top right of the screen.
If you clamp them between -1 and 1, it will always be on screen.