1

I am trying to add a custom view programmatically to my view controller. I used this snippet of code with no success of it appearing in front of my main view controller.

 var DynamicView = CustomFilter()
    DynamicView.setTranslatesAutoresizingMaskIntoConstraints(false)



    self.view.addSubview(DynamicView)

CustomFilter Class:

import UIKit

class CustomFilter:

UIView {
@IBOutlet weak var party: UIButton!
@IBOutlet weak var outdoors: UIButton!
@IBOutlet weak var sports: UIButton!
@IBOutlet weak var diner: UIButton!
@IBOutlet weak var music: UIButton!
@IBOutlet weak var gaming: UIButton!

}

The custom filter is connected to a xib file.

Xib File:

enter image description here

Is there a possibility that the custom view maybe out of placed? I'm not sure what I'm doing wrong.

2
  • 1
    If you want to load a view from a xib... you have to load it from the xib... Commented Jul 20, 2015 at 0:46
  • @nhgrif I want to load a xib on top of my view controller. I'm not sure how I can do that. Sorry my question was kind of unclear. Commented Jul 20, 2015 at 0:50

2 Answers 2

2

In order to use a view we designed in a xib, we most load from the xib.

if 
    let bundle = NSBundle(forClass: CustomFilter.self),
    let nib = bundle.loadNibNamed("<#Xib File Name#>", owner: self, options: nil),
    let dynamicView = nib.first as? CustomFilter {

        self.view.addSubview(dynamicView)
}

An alternative approach would be to write your CustomFilter's init to load the view from the xib itself.

More clearly, the problem you're having is that none of your CustomFilter's initializers are going to care about the xib file you made unless you write them and tell them to care about it. Your current code is returning a 0x0 view with probably a white or clear background. If you modified your current code to set the CustomFilter's frame to something other than 0x0 size and set the background color to something like UIColor.greenColor(), you'd see it clear as day.

Also, you could use Xcode's visual debugger to find it.

Sign up to request clarification or add additional context in comments.

Comments

0

It's probably zero height and zero width, and may be off-screen also.

You need to give it height and width constraints and x and y position constraints.

You should probably also use CustomFilter(frame: someFrameRect), since as I recall initWithFrame is the designated initializer for UIView.

As an aside, variable names should start with a lower-case letter, so DynamicView should be dynamicView

3 Comments

There's nothing wrong with using the zero-argument initializer. There's not much point in passing a frame rect when you're going to add autolayout constraints that probably alter the frame anyway...
I tried your suggestion. The view still doesn't appear. Maybe it is behind my viewcontroller , is that possible?
@htjohn No. It's not possible. A view controller is not part of the user interface at all. It's view property is, however you're adding your view as a subview of the view controller's view. A view can never be behind its parent (or any of its grandparents).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.