ios – The right way to reuse views extra successfully and elegantly?

0/5 No votes

Report this app

Description

[ad_1]

I’ve a web page(UIView) with full content material, and the opposite two pages contained significantly content material of this UIView.

I need to code a view that completes the above three pages.The view goes like this:

The Full Content material:
The full content

The second web page that with out buttons:
without button

The Third web page that with out Prime view:
The Third page that without Top view

I utilizing SnapKit to format subviews, my strategy now’s format full content material first,then hidding undesirable view and remake constraints.

func configWithoutButton(){
    self.middleButton.isHidden = true
    self.bottomButton.isHidden = true
    centerView.snp.remakeConstraints { make in
        make.prime.equalTo(topView.snp.backside).offset(16)
        make.left.proper.equalToSuperview()
        make.peak.equalTo(150)
    }
}

func configWithoutTopView(){
    self.topView.isHidden = true
    middleButton.snp.remakeConstraints { make in
        make.prime.equalToSuperview().offset(80)
        make.left.proper.equalToSuperview()
        make.peak.equalTo(40)
    }
}

It must be remake constraints and hidding heart view when (conceal/present heart view) button clicked. I implement it like this:

@objc func hideOrShowCenterView(){
    if(centerView.isHidden){
        centerView.isHidden = false
        bottomButton.snp.remakeConstraints { make in
            make.prime.equalTo(centerView.snp.backside).offset(16)
            make.left.proper.equalToSuperview()
            make.peak.equalTo(40)
        }
    }else{
        centerView.isHidden = true
        bottomButton.snp.remakeConstraints { make in
            make.prime.equalTo(middleButton.snp.backside).offset(16)
            make.left.proper.equalToSuperview()
            make.peak.equalTo(40)
        }
    }
}

There are my questions:

  1. I discovered this manner is tough to debug, and never factor sufficient(as a result of I would like to repeat/paste a number of line code) Is there a extra elegant strategy to implement this?
  2. It is a powerful strategy to deal with logic if the case quantity goes up. Any higher strategy to reuse the code if the case quantity is larger than 3?
  3. May you share your code if you meet the situation that views will dynamically change?

Suggestions:

  1. you’ll find the total supply code at this:Supply code hyperlink
  2. That is simply an instance of actual mission, the actual mission is extra advanced(eg. Prime view and heart View accommodates a number of subviews and must dynamically alter the peak of subviews. extra transition happens when consumer work together)

[ad_2]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.