Gheyb means for something to be hidden from one’s senses, and shahadah means openness and visibility.
One thing can be gheyb for a person, while being shahadah for another. It depends on the individual’s existential boundaries and how much of the existential world they cover and encompass in knowledge. But keeping in mind the limited amount of encompassment of other than Allah (swt) and at the same time, His complete encompassment of everything, one can conclude that it is only Allah (swt) who knows of the unseen, because He is the only one to encompass everything in this existential world and nothing leaves His existential boundaries, and no one and thing can keep itself unknown and hidden from Him [even if is of the unseen].
Therefore, He is the only one who has complete knowledge of both the gheyb and shahadah, and in reality, everything is considered shahadah to Him; no entity possesses such a level of knowledge. As for others, if they want to be aware of some of the unseen, it depends on their existential capacity and comprehension; they might become aware of some things while remaining totally uninformed or learned at a very low level regarding other secrets.
Of course, it must be noted that allocating knowledge of the unseen to Allah (swt) doesn’t mean that others can't learn of some of these secrets as a result of their worthiness and through Allah's (swt) teaching. This is a blessing that Allah (swt) bestows upon certain chosen servants of His, such as the prophets and apostles. This is why we see the prophets speaking of revelation, which is considered of the unseen, or we see them foretelling future incidents that actually do take place at the time they have predicted.
Keeping in mind what was said, one won't see any contradiction between verses and hadiths that say knowledge of the unseen only belongs to Allah (swt), and verses and hadiths that assert that the prophets and imams were also aware of the unseen. Harmony can be established between these two groups of verses and traditions like so:
1- When it is said that knowledge of the unseen only belongs to Allah (swt), it means that it originally belongs to Him and He is independent in possessing it, while others are dependent on Him and His teaching for learning from it.
2- It is only Allah (swt) who knows every detail of the unseen, while others can only become aware of some of it and only to some extent.
3- Allah (swt) is aware of all secrets of the unseen at the very moment, while others aren't aware right now, although it is possible for them to learn in the future and will only become aware if Allah (swt) wills and informs them. Imam Sadiq (as) has been narrated saying: “Whenever the imam wishes to know something, Allah (swt) teaches him.”
As for verse 34 of surah Luqman, it says: “Indeed the knowledge of the Hour is with Allah. He sends down the rain, and He knows what is in the wombs. No soul knows what it will earn tomorrow, and no soul knows in what land it will die. Indeed Allah is all-knowing, all-aware”.
There are two interpretations regarding this verse:
1- The five things mentioned in the verse, are all of the unseen that no one is aware of other than Allah (swt) and those whom he wishes to know. Sending down rain doesn’t just refer to its pouring down from the sky that weather forecasters can foretell, what is meant is precisely where, when and how much it is going to rain; such knowledge is only in Allah's (swt) possession and the only thing others can do is to try to guess each of them. The same goes for the fetus in the womb; what Allah (swt) means isn't that He only knows of its gender, what He is saying is that He is aware of everything pertaining to it: its gender, health, physical and spiritual traits and even its potentials and whether it will grow to be one of those who reaches salvation, or damnation. Clearly, such knowledge is only in Allah's (swt) possession. Now, if there are hadiths that say that the imams bear some of this knowledge, it is because there is no contradiction between having a portion of this knowledge through Allah's (swt) teaching, and the detailed knowledge of it belonging only to Allah (swt). Add to that the fact that they aren't independent in possessing this knowledge; it has been granted to them by Allah (swt) in an amount that He sees fit.
2- The other explanation of this verse is that in this verse, only the part that speaks of the time of the Day of Judgment is considered of the unseen knowledge, and Allah (swt) considers Himself to be the only one to know of it, and as the verse goes on, its tone changes saying that Allah (swt) sends rain and is aware of what is in the wombs of mothers, without mentioning anything about this knowledge only belonging to Him. On this basis, if in the future it becomes possible to precisely pinpoint where it is going to rain and when and its amount, there will be no contradiction with the verse. Also, in the fourth and fifth part of the verse, it has been stressed that no one knows of exactly where and when he/she will die. This not knowing is in exact accordance with the law of nature that says no one essentially knows of the unseen and future events and incidents. Nevertheless, this doesn’t disprove that Allah (swt) may sometimes inform some people of the future and as to where and when they will die.
Three key points need to be made in order to answer your question thoroughly:
1- What is the Unseen?
Gheyb or unseen, means for something to be concealed from the senses. It is defined as: الغیب: کل ما غاب عنک; meaning that anything that is hidden from you is considered gheyb and unseen. This is why when the sun sets, Arabs say: “غابت الشمس”; meaning that the sun became hidden and unseen.
When this term is used as the opposite of shahadah or “the seen”, which is a Quranic and theological term, it refers to something metaphysical and outside this natural world that can't be sensed with the normal senses and if one ever wants to actually sense it, he will need to use other methods.
Allamah Tabatabai says: Gheyb and Shahadah are two proportional concepts, meaning that something might be gheyb in relation with one thing, while being shahadah in relation with another; the reason being that all things in this world have their boundaries they cannot be separated from. Therefore, anything that comes inside their boundaries will be subject to their comprehension, thus making them shahadah, because they are something that is comprehended by them. On the other hand, anything that doesn’t enter their boundaries will be gheyb, because it is something that isn't subject to their comprehension.”
2- Who is aware of the gheyb (unseen) and possesses its knowledge?
What the explanation of gheyb given above entails, is that knowledge of the gheyb and being aware of the unseen, is something that only Allah (swt) completely possesses, because He is the only one who has total dominion and awareness of the entire universe and everything it encompasses; nothing lays outside His existential boundaries, and nothing can conceal itself from Him. This subject has been pointed to in different ways by the Holy Quran:
“Allah is the Knower of the Unseen and The Visible; and He is the Supreme Exalted Great”
“They say:" Why was not a Miracle Sent down upon him from his Creator and Nurturer?" Say [O, Messenger!]:" The knowledge of the Unseen is only For Allah”
“Say," No one in the heavens or the earth knows the Unseen except Allah”
The first thing one understands from such verses is that no one other than Allah (swt) is completely aware of the unseen, nevertheless, there are verses that state that there are others of those who are apostles of Allah (swt) who also know some things regarding it:
“Allah will not acquaint you with the Unseen, but Allah chooses from His apostles whomever He wishes [to inform of the unseen]”
“Knower of the Unseen, He does not disclose His Unseen to anyone, except to an apostle He approves of”
When speaking of Prophet Isa’s (pbuh) miracles, the Quran says:
“And I will tell you what you have eaten and what you have stored in your houses”
In addition to these verses, there are also many hadiths that show that the prophet (pbuh) and imams (as) have some knowledge of the unseen; this knowledge being in two forms:
The first form being when the prophet (pbuh) would inform of revelation being revealed; this itself is a form of the unseen which he had.
The second form: The precise prophecies they made, like when the prophet (pbuh) informed of the battle of Mutah (موته) and of the martyrdom of Jafar and other commanders of the Islamic army while he was in Medinah with the people, right when it all happened. In many hadiths, the knowledge of the imams regarding the unseen has been emphasized. One can claim that these hadiths even reach the degree of tawatur.
As for how the hadiths and the second group of verses mentioned (that imply that others other than Allah (swt) are also aware of the unseen) aren't in contradiction with the first (that imply it is only Allah (swt) who has knowledge of the unseen), one can say the following:
1- Knowledge of the gheyb originally and primarily belongs to Allah (swt). He independently possesses it without anyone else having given it to Him, but others who might possess some of it, never independently obtain it, it is always bestowed upon them by Allah (swt), making them dependant on Him for it.
2- Allah (swt) is the only one to know every single detail of the unseen, while others only have a general overview.
3- Allah (swt) knows of all secrets of the unseen at the very moment and others aren't, yet only if Allah (swt) wills, they can be informed as well, or if they will, given that their will is backed by Allah's (swt). Imam Sadiq (as) has been reported saying: “Whenever the imam wishes to know something, Allah (swt) teaches him.”
In addition to this, there are verses that speak of a specific type of knowledge that is obtained through self-building and practicing religion and spirituality and movement towards the truth. Verses like: “O you who have faith! If you are wary of Allah, He shall appoint a criterion for you [enabling you to distinguish between right and wrong], and absolve you of your misdeeds, and forgive you, for Allah is dispenser of a great grace”.
Also, when Imam Ali (as) would recite the verse: “…by men whom neither trading nor bargaining distracts from the remembrance of Allah…”, he would say: “Certainly, Allah (swt), the Glorified, the Sublime, has made His remembrance the light for hearts which hear with its help despite deafness, see with its help despite blindness and become submissive with its help despite unruliness. In all the periods and times when there were no prophets, there have been persons with whom Alláh, precious are His bounties, whispered through their wits and spoke through their minds”.
3- The third central point of this discussion:
Now we will engage in discussing a bit about verse 34 of surah Luqman:
“Indeed the knowledge of the Hour is with Allah. He sends down the rain, and He knows what is in the wombs. No soul knows what it will earn tomorrow, and no soul knows in what land it will die. Indeed Allah is all-knowing, all-aware”.
There are two explanations for this verse:
1- This verse correlates with the verse preceding it which speaks of the hereafter; it speaks of things that are of the unseen belonging only to Allah (swt), which we call “knowledge of the unseen”. This verse points to five things that are all of the unseen:
a) Knowing when the hereafter will take place: This is something the mushriks have asked the prophet (pbuh) about many times: “When is the hereafter?” The Quran answers them saying: “Indeed the Hour is bound to come: I will have it hidden, so that every soul may be rewarded for what it strives for”.
b) When it will rain: The knowledge of when and where it will exactly rain and how much, are all in the possession of Allah (swt), and others can only guess about them.
c) What is in the womb of mothers: Meaning the gender and inner and outer characteristics of the fetus. Therefore, although there are ways of finding out its gender, yet there is no way to find out about its physical and spiritual traits, potentials and thousands of other characteristics that it has; all of this is only known of by Allah (swt). Also, the future of the fetus and whether it will reach salvation or damnation in this world are all things that only Allah (swt) can know of.
d) Future incidents and their details and what each person will do in the future.
e) And finally, where every person will die.
According to this explanation and interpretation or tafsir, all of the above are considered part of the unseen, whose knowledge only belongs to Allah (swt). If one asks: “Then how come some of the above were known by the imams according to hadiths?”, the answer will be that knowing only a portion of them through Allah's (swt) teaching is in no contradiction with the exclusive allocation of their detailed knowledge to Allah (swt). At the same time, they (the infallibles) aren't independent in their knowledge, but are totally dependent on Allah's (swt) teaching, to the amount He sees fit.
2- Some sections of this verse differ from others; their way of speaking and tone varies. The first phrase of this verse tells us that the knowledge of when the hereafter will take place only belongs to him and no one else is aware of it. According to Arabic literature rules, this exclusiveness can be concluded by the fact that the word “عنده” has been mentioned before “علم الساعة”. Other verses also stress that such knowledge only belongs to Allah (swt).
But in the second section of the verse, we see that the verses style of speech changes and there is no implication of exclusiveness. What can be concluded from this part is that Allah (swt) is aware of when it will rain and what is in the wombs of mothers, without anything being said about anyone else knowing or not.
Therefore, Allah's (swt) knowledge regarding when it will rain and the fetus in the womb of the mother, in no way means that certain servants of His also can't learn of them in other ways such as divine revelation and inspiration. The only difference between Allah's (swt) knowledge and their’s being that His originally belongs to Him, while others acquire and obtain it and it doesn’t essentially belong to them.
In the fourth and fifth parts of the verse, its tone changes even more. They mainly speak of the fact that Allah (swt) knows where each person will die. Not knowing where one will die is in conformity with nature’s laws that say no one essentially knows of the unseen and what is going on on the other side, nevertheless, this doesn’t mean that Allah (swt) can't inform any of His servants of their future and where they will die.
To sum it all up, this second interpretation of the verse says that with help from other verses and the first part of this verse, one concludes that knowing of when the hereafter will take place is something that only Allah (swt) knows of and no one else.
 Seyyid Ali Akbar Qureyshi, Qamuse Quran, vol. 5, pg. 133.
 Ibn Fares, Mu’jam Maqayisil-Lughah, root word “غیب”.
 Al-Mizan, vol. 11, pg. 418.
 Ale-Imran:179 This verse implies that Allah (swt) his messengers of the unseen.
 Ibn Athir, Al-Kamel fil-Tarikh, vol. 2, pg. 237.
 See: Kuleini, Usul Kafi, vol. 1, there are different chapters on this subject; Muhammad Baqir Majlisi, Biharul-Anwar, vol. 26.
 Tawatur is a hadithic term referring to a certain concept or story being repeated many times in different hadiths, to the extent that one becomes sure that it isn't possible for all narrators of the hadiths to have been mistaken or lying.
 See: Usul-Kafi, vol. 1, chapter “ان الائمة اذا شاؤوا ان یعلموا علموا”, hadith 3.
 Nahjul-Balaghah, sermon 220.
 This is the verse itself:
“الله عنده علم الساعة و ینزّل الغیث و یعلم ما فی الارحام و ماتدری نفس ماذا تکسب غداً و ماتدری نفس بایّ ارض تموت انّ الله علیم خبیر”