2016-07-25 10 views
1

と呼ばれるUIの作成:スウィフト:私はここに、これらのビューのいずれかを持っている機能が

let placeContainerView: UIView = { 
    let view = UIView() 
    view.backgroundColor = UIColor.whiteColor() 
    view.translatesAutoresizingMaskIntoConstraints = false 
    view.layer.cornerRadius = 7 
    view.layer.masksToBounds = true 

    return view 
}() 

をそして、私はそれを次のものに機能「createNewViewが()」と呼ばれるたびに作成していますよ。

let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(showFullPlaceContainerViewFunction)) 
    placeContainerView.addGestureRecognizer(showFullPlaceContainerView) 

私はplaceContainerViewのそれぞれに応答する必要があります。

私はビューが生成されるようにしたい、私はそれに特定の値を与えます。そして、関数createNewView()を呼び出すと、その隣に新しいビューが表示されます。これは、新しい値を入れた場合を除いて全く同じです。

私に知っておいてください!

次のコードは、私がセットアップplaceContainerViewたびにしたい方法を示していますが、私は彼らがplaceContainerView.topAnchorは()毎回異なるように表示する必要がある..:

EDITはありがとうそれが自分のクラスに保持され、それが何回作成されたのかわからない場合、それはどのように正確に機能しますか?

また、placeContainerViewにはplaceLabelとplaceImageViewが含まれているため、これらは新しいPlaceContainerViewClass内に生成する必要がありますか?

func setupPlaceContainerView() { 
    placeContainerView.leftAnchor.constraintEqualToAnchor(view.leftAnchor, constant: -180).active = true 
    placeContainerView.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 80).active = true 
    placeContainerView.widthAnchor.constraintEqualToConstant(227).active = true 
    placeContainerView.heightAnchor.constraintEqualToConstant(45).active = true 


    placeContainerView.addSubview(placeLabel) 
    placeContainerView.addSubview(placeImageLabelSeparator) 
    placeContainerView.addSubview(placeImageView) 

    placeLabel.leftAnchor.constraintEqualToAnchor(placeContainerView.leftAnchor).active = true 
    placeLabel.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true 
    placeLabel.widthAnchor.constraintEqualToConstant(180).active = true 
    placeLabel.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true 

    placeImageLabelSeparator.leftAnchor.constraintEqualToAnchor(placeLabel.rightAnchor).active = true 
    placeImageLabelSeparator.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true 
    placeImageLabelSeparator.widthAnchor.constraintEqualToConstant(2).active = true 
    placeImageLabelSeparator.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true 

    placeImageView.leftAnchor.constraintEqualToAnchor(placeImageLabelSeparator.rightAnchor).active = true 
    placeImageView.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true 
    placeImageView.widthAnchor.constraintEqualToConstant(45).active = true 
    placeImageView.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true 
+0

まず、計算されたプロパティではなくUIViewのサブクラスを作成します。次にインスタンス – Paulw11

答えて

1

@ Paulw11が指摘したように、この作業は新しいクラスを作成することで簡単に実行されます。 ViewControllerを内部

class placeContainerView:UIView { 

    var x:Double! 
    var y:Bool! 
    var z:UILabel! 
    var controller:UIViewController! 

    //If you want to pass specific values number, you can use convenience init method OR you can use the default init method they give you. 
    //previousLabelFrame:CGrect = CGRect() // I defaulted all these values to 0, make them whatevree u need. You would use the default one for the first Label you would make. Then after that, you would pass in the previous one made, to get the frame of it so you can add to the one after that. 
    convenience init(x:Double,y:Bool, z:UILabel, previousLabelFrame:CGRect = CGRect(x: 0, y:0, width:0, height:0), VC:UIViewController) { 
     self.x = x 
     self.y = y 
     self.z = z 
     self.controller = VC 
     let distance = self.controller.width*0.1 //Whatever u decide here 
     //You could just do CGRect(x:previousLabelFrame.maxX+distance, depeding on what you need. 
     self.frame = CGRect(x: previousLabelFrame.minX+distance, y:previousLabelFrame.minY, width: previousLabelFrame.width, height:previousLabelFrame.height) 
    } 

} 

使用法:

var views:[placeContainerView] = [] 

let view:placeContainerView = placeContainerView(10, true, UILabel(),views[views.count-1], self) 
self.views.append(view) 
//OR if this is the FIRST placeContainerView of the whole app, it will use the default values for the frame. 
let view:placeContainerView = placeContainerView(10, true, UILabel(), self) 
self.views.append(view) 

使用する方法のいくつかの奇妙な例。 ボタンをクリックするたびに、新しいplaceContainerViewを作成します。

+0

が必要なときに、適切な 'CGRect'をサブクラスの' init(frame:) 'Initializerに渡すことができます。元の投稿にいくつかの追加情報を追加して、私のさらなる混乱の一部を強調しました。 – tryingtolearn

+0

func createNewViewはいつ呼び出されますか? Thatllは私にこれを説明するのを助ける – impression7vx

+0

ユーザーが何かを検索バーに入力するときcreateNewView()が呼び出されます。 placeContainerViewには、検索バーに入力された内容を表示するUILabelがあります。それは単に誰かの検索を記録する方法です。 – tryingtolearn

関連する問題