lostcoder
lostcoder

Reputation: 5

Create UIContainerView programmatically

I've just started to code my app in Swift 2 and avoiding the use of XIBs and storyboards.

However, I am unable to replicate the following feature. It's exactly what I wanted.

I've tried creating a UIView to perform the following using .backgroundColor and it works, however, I am unable to link it to my UIViewControllers. Just wondering how is it done? How do I link my UIView to my UIViewController?

Codes:

let subFrame : CGRect = CGRectMake(0,screenHeight*1/2.75,screenWidth,screenHeight)
var loginView = SignUpViewController()

let signUpView: UIView = UIView(frame: subFrame)
signUpView.backgroundColor = UIColor.redColor()

//Controls what each segment does
switch segmentView.indexOfSelectedSegment {
case 0:
       self.view.addSubview(signUpView)
case 1:
      self.view.addSubview(loginView)
default:
       break;
}

I'm not even sure if .view.addSubview(xxx) overwrites/replaces the original subview if it is not this way. Is this the right way to do it?

Upvotes: 0

Views: 231

Answers (1)

DevAndArtist
DevAndArtist

Reputation: 5169

Do not just start coding an app if you are not familiar with simple things of the OOP (Object-Oriented-Programming) language like Swift. This is not the way how to learn a programming language. Sure you could learn while experimenting but it is better to understand the book first before starting with more complex stuff. Read a few more pages of the Swift book from Apple. Most classes for iOS development are still Objective-C wrapped classes (reference type because the top superClass is probably NSObject; keep this in mind).

Here is the code example you wanted:

class ViewController: UIViewController {

    let firstView = UIView()
    let secondView = UIView()
    let segmentedControlView = UISegmentedControl(items: ["firstView", "secondView"])

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor() // we need this for the playground

        /* setup your view here */
        /* add your sigment logic somewhere */

        self.view.addSubview(self.segmentedControlView)
        self.view.addSubview(self.firstView)
        self.view.addSubview(self.secondView)

        self.segmentedControlView.frame = CGRect(x: 0, y: 20, width: self.view.frame.width, height: 44)
        self.segmentedControlView.selectedSegmentIndex = 0 // enable the first segment
        self.segmentedControlView.addTarget(self, action: "segmentIndexChanged:", forControlEvents: UIControlEvents.ValueChanged)

        /* add your own frame calculation here */
        /* I prefer AutoLayout, but for the example static frames will be fine */

        self.firstView.frame.origin = CGPoint(x: 0, y: self.segmentedControlView.frame.origin.y + self.segmentedControlView.frame.height)
        self.firstView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height - self.segmentedControlView.frame.origin.y)
        // to prevent same code, we just copy the same frame from the firstView
        // both will sit in the same place
        self.secondView.frame = self.firstView.frame

        /* lets add some colors so we'll see our views */
        self.firstView.backgroundColor = UIColor.blueColor()
        self.secondView.backgroundColor = UIColor.redColor()

        self.secondView.hidden = true // when intializer the secondView is not visible
    }

    func segmentIndexChanged(sender: UISegmentedControl) {

        switch sender.selectedSegmentIndex {

        case 0:
            self.firstView.hidden = false
            self.secondView.hidden = true

        case 1:
            self.firstView.hidden = true
            self.secondView.hidden = false

        default:
            break;
        }
    }
}

If you do not understand a function, should should look up its definition in the developer docs. (Like: addSubview)

Upvotes: 1

Related Questions