2016-04-26 16 views
0

CALayer、contents = NSImageを持つNSView(myImage)を持っているため、NSImageに正確に合うようにサイズが変更されました。 これは別のNSView(myContainer)の内部にあり、両方の軸で独立して伸縮するように固定されています。NSImageViewでNSViewをNSImageのように動作させるためのレイアウト制約

だから私はこれをNSImageViewのように振る舞うのが大好きです。 "myContainer"ビューの範囲内で完全に表示されたままにしておき、NALMageをそのCALayerに追加したときに作成されたサイズより大きくなることはありません。

IBでのみ可能ですか?わかりませんが、アプリケーションが実行されているときにIBによって作成された制約と制約が生成され修正されたとしても、NSImageViewと同じように動作させることはできません。助けてください。 PROGRESS

func newUserImageInputDidArrive() -> Void { 
    // the real widh and height of the NSImage we will add to layer of NSView 
    currentImageRealWidth = (mainImageDropZone.image?.size.width)! 
    currentImageRealHeight = (mainImageDropZone.image?.size.height)! 
    // set the width of NSView to match above 
    mainImageView.frame.size.width = currentImageRealWidth 
    mainImageView.frame.size.height = currentImageRealHeight 
    // create the layer in the NSView 
    mainImageView.wantsLayer = true 
    mainImageView.layer?.borderColor = NSColor.redColor().CGColor 
    mainImageView.layer?.borderWidth = 5 
    // so the image added to the layer will resize proportionately within the bounds of the layer 
    mainImageView.layer?.contentsGravity = kCAGravityResizeAspect 
    // add the image 
    mainImageView.layer?.contents = mainImageDropZone.image 
    // so now NSView's layer has the image and is the size of the origonal image 
} 

層とのNSView WITH

EDITは現在、スーパーへのすべての側面に固定されています。 これは、NSImageビューと同じ動作を「比例して上下にスケール」します。しかし、私は "比例してスケールダウン"したいだけです。 レイヤーを保持するNSViewの幅と高さを、レイヤーで使用されている元の画像から取得した最大幅と高さに制限する必要があると感じました。 しかし、これを達成する方法は私を暗示しています。

enter image description here

答えて

0

はパズルうちの最後のピースを考え出し。 私の質問で上記のコードと結合: (currentImageRealWidthは、ユーザーが画像を追加したときに変数として格納されます)。

override func viewWillLayout() { 
    if(mainImageView.layer != nil){ 
     if(mainImageView.layer?.frame.width > currentImageRealWidth){ 
      conMainImageViewLeading.constant = (mainContentView.frame.width - currentImageRealWidth)/2 
      conMainImageViewTrailing.constant = (mainContentView.frame.width - currentImageRealWidth)/2 
     }else{ 
      conMainImageViewLeading.constant = 0 
      conMainImageViewTrailing.constant = 0 
     } 
    } 
    super.viewWillLayout() 
} 
関連する問題