adding subviews in uiviewcontroller make it goes will - ios

I have a custom UIView that I need to add as a subview in a UIViewController.
But if I use [self.view addSubview:newView]; the app goes in a infinity loop and doesn't start. But if I use self.view = newView then it works. But I need it as a subview.
The UIView contains a grid layout of custom button.

I guess you do that in the loadView method. You must not call the view method of the view controller in loadView because that will itself call loadView! However calling setView: (e.g. self.view = newView) is okay.
My suggestion is to add the subview in viewDidLoad.

Related

Scroll view not working in drag and drop approach. why?

If we add scroll view to storyboard it will visible on VC, but it not scrolled Why? Can any one please tell me the answer......
To make it work you need to do following.
Set the delegate property of your UIScrollView.
Set its contentSize.
Make your controller to follow the delegate of UIScrollView by adding <UIScrollViewDelegate>. in your class interface declaration.
I hope you have created your UIScrollView outlet then add this code in your viewDidLoad method.
self.myScrollView.delegate = self;
[self.myScrollView setContentSize:CGSizeMake(WIDTH, HEIGHT)];

How can I add a common view to multiple ViewControllers in iOS Swift?

I'm kinda new to iOS development.
I want to have a custom View and some Labels inside it. And In some viewControllers of my app on a button click I want to add/show that View
at the bottom of that viewController.
As far as I'm manually adding the view in storyboard in all the viewControllers in which I want the view to display. But this is not efficient. How can I add this view in viewControllers programmatically on button click?
Make one BaseViewController class inherited with UIViewController
Now create method named as designFooter in BaseViewController
func designFooter() {
var viewFooter: UIView = UIView(frame: CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50))
viewFooter.backgroundColor = UIColor.redColor()
self.view!.addSubview(viewFooter)
}
For Swift 4, 5.1:
func designFooter() {
let viewFooter: UIView = UIView(frame: CGRect(x:0, y:self.view.bounds.size.height - 50, width:self.view.bounds.size.width, height:50))
viewFooter.backgroundColor = UIColor.red
self.view.addSubview(viewFooter)
}
Now inherit this BaseViewController to you ViewController where you want to add footer, and on button click just call self.designFooter()
If this subview you want to add has some dynamic content or has much of its own logic, you might want to employ view controller containment, specifically not only add a subview, but add a controller associated with that subview, too. So, you can have a scene in your storyboard for this subview that will appear on the bottom, and associate it with its own view controller. Then, when you want to add it, you'd do something like:
let child = storyboard!.instantiateViewController(withIdentifier: "storyboardid")
addChild(child)
// set the `frame` or `constraints` such that it is in the correct place, perhaps animating it into place
view.addSubview(child.view)
child.didMove(toParent: self)
And when you want to remove it:
child.willMove(toParent: nil)
child.view.removeFromSuperview()
removeChild(child)
Personally, if this can really appear and disappear from any scene in my app I actually embed the whole app in a container view controller. Then this popping of the child in and out only has to be done once, on this master container view controller.
For example, consider this storyboard:
That is an embedded "container view" (the storyboard equivalent of view controller containment, discussed above). And I can then have a label animate in an out (by animating layoutIfNeeded after changing the height constraint of some view with a label). Then, this bottom view can animate in and out regardless of which view controller's view is currently visible:
Just create a UIView and call addSubview:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//change this frame for your purposes
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10,10,50,10)];
[l setText: #"My label"];
[view addSubview: l];
[self.view addSubview: view];
As far as adding this to multiple view controllers... I suppose if you had a lot of different view controllers with this same UIView you could create a subclass of UIViewController called CustomViewController. In that class add the above code to viewDidLoad. Then, subclass CustomViewController in all the view controllers with this particular view, and they will automatically add it for you.
Edit:
If you want to design the view in interface builder, make a custom subclass of UIView. Let's call this CustomView. Design it in a nib and add any code you want. Then, whenever you want to create that view, simply call CustomView *cv = [[CustomView alloc] initWithFrame:...] and then do [self.view addSubview:cv];

Calling loadView method to reload the uiview

I have a case where all my subviews of a viewcontroller (say A) are declared in the loadView method. When I remove the viewcontroller's view (say B) from the superview and add back viewcontroller A's view to the superview, how can I reload the view? Subview B is on top of subview A and when I remove B I should be looking at an Update subview A.
Simply removing the view doesn't destroy it. But if you have destroyed the view (by calling [viewcontroller setView:nil]), then simply calling [viewcontroller view] will reload the view. You should never call loadView - the system will do it for you.

Why do I get a black screen when I insert a subview?

Using the following code to insert a subview in my UIView I get presented with a black screen. Can someone tell me why?
CJGBoardLabels* whiteLabels = [[CJGBoardLabels alloc] initWithFrame:self.frame
andOrientation:#"white"
andOrigin:self.boardOrigin
andSquareSize:_squareSize
andHorizontalOffset:_horizontalOffset
andVerticalOffset:_verticalOffset];
[self insertSubview:whiteLabels aboveSubview:self];
I get the same result with the following line too:
[self insertSubview:whiteLabels belowSubview:self];
Don't add subviews in the drawRect: method. Instead, use the actual triggering method to add the subviews, and preferably get the controller to add the subviews (not the view itself). So, call addSubview: in the controllers viewDidLoad or an action method.

Can I add a subview inside a subview?

I have added a view inside ViewController using [self.view addSubview:newView]. newView is a UIView type object.
Inside newView I added added a few views using [self addSubview:polyg];.
Polygs also inherit from UIView. But they never get drawn. drawRect:(CGRect)rect is never called.
I'm new to iOS and have no idea what I'm doing wrong.
It probably has something to do with the fact that I did not use self.view when adding views to newView. I can't even use self.view inside newView.
how can I make this work?
edit:
When i create these polyg objects I'm using initWithFrame and sending newView's frame, like this: [[Polyg alloc] initWithFrame:self.frame];
Could this be the cause? Polyg is going to be a movable and resizable polygon, so I figured each should be able to draw on the entire screen.
edit:
I created a method inside newView called touchX:Y that is called from the viewController whenever I touch my finger on the screen. Inside I wrote this: NSLog(#"subview = %i", self.subviews.count);. Whenever I touch the screen I see this on the console: subview = 89 so I'm pretty sure the subviews were added to newView.
I tried adding this to the same touchX:Y method:
for (UIView* subview in self.subviews) {
[subview setNeedsDisplay];
}
But the subviews' drawRect is never called.
The most likely possibility is that newView is either completely off the screen or have a size of (0,0). Right before you add the ployg subviews, use the debugger to print out the frame of newView.

Resources