Is the length header used to indicate the end of a segment?
"segment" is the wrong term with UDP since it suggests that it is part of something bigger which gets eventually reassembled. UDP has the concept of a datagram or a message and such datagrams should be considered separate entities. This means that they can be lost, duplicated or reordered and UDP does not define what happens in this case. Dealing with this is up to the application.
... then why can't we use a special bit pattern to indicate the end of a segment?
This would be way more complex and time consuming to process than just prefixing the data with its length. It would mean that one would need to introduce a mechanism to either escape data which accidentally match the bit pattern or encode all the data in something like base64 (which is a waste of space compared to pure binary). It would also mean to actively scan the whole payload for the bit pattern and rewrite everything which was escaped or (in case of base64) to decode the data again.