2016-07-25 2 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는() 할 때마다 다른 있도록 표시해야 .. :

편집 감사합니다 자체 클래스에 보관되어 있고 생성 된 횟수를 모르는 경우 정확히 작동합니까?

또한 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의 하위 클래스를 만듭니다. 그런 다음 인스턴스가 필요할 때 서브 클래스의'init (frame :)'Initializer에 적절한 CGRect를 전달할 수 있습니다. – 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

당신을 얻을 수 있습니다. 원래 게시물에 몇 가지 추가 정보를 추가하여 혼란을 부각 시켰습니다. – tryingtolearn

+0

func createNewView는 언제 호출됩니까? Thatll 내가 이것을 설명하는 데 도움이 – impression7vx

+0

사용자가 검색 창에 뭔가를 입력하면 createNewView()가 호출됩니다. placeContainerView에는 검색 막대에 입력 된 내용을 표시하는 UILabel이 있습니다. 그것은 단지 누군가의 수색을 기록하는 방법 일뿐입니다. – tryingtolearn