2015-08-25 46 views
1

下打印屏幕顯示我的佈局:如何將子視圖的子視圖置頂?

enter image description here

我的主要觀點,然後頂視圖,然後圖片。

當我按在圖片上,我希望出現在整個視圖半transaprent黑屏:

 let scrennRect = UIScreen.mainScreen().bounds 
     let coverView = UIView(frame: scrennRect) 
     coverView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) 
     self.view.addSubview(coverView) 

但我想我的照片留在coverView的頂部?
有沒有一種方法可以只把圖片放在coverView前面?

+0

是的,但imageView其內部頂視圖不self.view,多數民衆贊成我的問題。 – Kobe

+0

它不工作,因爲我有這個:mainView - > coverview - > topview - > imageView ....頂視圖需要將imageview帶到頂部,然後以某種方式覆蓋視圖。 – Kobe

+0

嘗試此示例:https://github.com/adrianstanciu24/coverView-test/blob/master/testuiimageview/ViewController.swift – Kobe

回答

3

問題是,您的coverView和imageView有不同的超級意見。

這就是爲什麼你不能把imageView放在前面。

爲了將imageView放在前面,您需要將它移動到頂視圖之外,以便您的視圖包含頂視圖和圖像視圖。
然後,您可以將coverView添加到主視圖,並將imageView放在前面。

另一種解決方案: 將圖像視圖放置在coverView中完全相同的位置。

+0

是的,這是我的問題,但將圖片移動到另一個視圖中,由於自動佈局和設置爲超級視圖的約束,對我而言並不適用。 – Kobe

+0

那麼只需在coverView中放置另一個圖像視圖,並在圖像視圖中設置拖尾的頂部和底部約束。您可以在界面構建器中輕鬆完成此任務,並將coverView的alpha值設置爲0,然後在代碼中將其設置爲1。 –

0

這應該做你的工作。任何視圖都會維護一堆子視圖。

self.view.addSubView(blackTransparentView); 
self.view.bringSubViewToFront(myImageView); 
+0

它不起作用。 self.view不直接包含imageView。 – Kobe

+0

無論你想要什麼,都要把它放在最前面。 –