stacking two UITableViews in a single view programmatically? - ios

i'd like to stack two UITableViews in a single view. i can do this in interface builder, however, i would like to do this programmatically in code.
in my main UIViewController, i create two UITableViewControllers in my viewDidLoad method.
i thought at first i could do this by:
[self.view addSubView:controller1.view];
[self.view addSubView:controller2.view];
but this only shows the 2nd view. i've also tried:
[self.view addSubview:controller1.view];
[controller1.view addSubview:controller2.view];
but it still doesn't do what interface builder did above.
UPDATE
i want to accomplish this:
two table views
which i did in interface builder:
xib
but in code.

What is the reason to create two UITableViewControllers? Simply create two UITableView objects and manipulate them.

Overlapped? Did you set their frame different, in order to see them?

controller1.view.frame = CGRectMake(0,0,320,75);
controller2.view.frame = CGRectMake(0,75,320,385);
In a rectangle the values are measured from the upper left corner: x,y,w,h.

Related

How to bring subview to front in interface builder without changing its position

I want to ask a simple question how can I temporary bring a subview to front to view its element without changing its position by dragging and dropping. The problem i face all the time is i forgot to put the views back to there position and that causes lot of trouble specially if you are working on the view that have large number of subviews.
Question : Is there any shortcut or any functionality that can show the view temporary without dragging, changing its frame or making any changes in its hierarchy.
For a view controller like this :-
FYI, for those who are wondering, the view closest to the bottom of the list (on the left) will show in the front. So in the case below, view 'a' will be in front of 'b' and 'b' will be in front of 'c'
XIB:
If you need your views to be readily accessible for viewing/editing without having to rearrange them, I would actually recommend breaking them out into their own view and then stitching them together in the correct order in your code. This will ensure that all elements will be put in the proper order and will always be easily editable. Something like this:
And then in your code, in somewhere like viewDidLoad:
[self.view addSubview:view2];
[self.view addSubview:view3];

What is the most standard approach to creating a separate menu which can be displayed over a single view controller

Apologize in advance as I'm still new to Xcode.
I have a 2D game build entirely in one viewController. I would like to add both a start menu and in game menu to the game. I assume the best way to do this is through another instance of a viewController? Any advice or suggestions would be very helpful.
First place a UIView on your Xib or UIStoryboard.
Put 2 buttons on it and make their connections and outlets.
The view you want to open is view_Menu.( let us consider)
Now for managing view you need to do this:
view_Menu.frame = CGRectMake(xAxis, yAxis, self.view.frame.size.width, 150);
[self.view addSubView: view_Menu];
In xAxis and yAxis you can put your values.
Now when you click any button inside the menu view you can simply call this method:
[view_Menu RemoveFromSuperview];
So it will help to provide you that you want. A bit of things that you can put a background image or make some custom animations for better look.

How to assign a higher level on a uiview in order to make it override another view?

I created a uiview and added a sub-uiview on it. Then I allow user to drag the sub-view around the screen freely. But I have problem when moving the sub-view on top of the screen. Please see below picture. The sub view override the top uiview on the screen. What I want is to let the top uiview to override the sub-view. How can I change the level of a uiview in this case?
when I use self.titleView.bringSubviewToFront(sv)
I see another problem as shown in below image. The status bar is not at the top of the image view.
You can use bringSubviewToFronton parent view to bring sub view to front as follow:
objective-C:
[parentView bringSubviewToFront:childView];
swift:
parentView.bringSubviewToFront(childView)
Edit:
use below method to manage views:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
like,
self.view.sendSubviewToBack(myView)
For more detail check: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW46
Hope this helps :)
parentView.bringSubviewToFront(view)
UPDATE
Pay attention: you must considered that parentView is the parent of your view. To explain better this concept if you have a line like this:
self.container.addSubview(view)
so self.container is the view parent's, dont confuse it with the view below your view...it may not be the same.
If your draggable view is a subview of the image view try to set masksToBounds to true:
imageView.layer.masksToBounds = True
But the reason of your issue is probably because you set zPosition of draggableView.layer to some positive number. Usually it is better to use methods such as insertSubview(_:aboveSubview:) and bringSubview(toFront:) to manage subview's order.

how to redraw custom view from the controller in ios objective-c

I have a custom view with 3 buttons taking full width.
I import the class to my controller where I want to hide on button (menuBtn) and make one of the one of the other buttons (searchbarBtn) be bigger to fill the empty space by doing:
`self.topMenuView.searchbarBtn.frame = CGRectMake(self.topMenuView.searchbarBtn.frame.origin.x, self.topMenuView.searchbarBtn.frame.origin.x, self.topMenuView.searchbarBtn.frame.size.width + self.topMenuView.menuBtn.frame.size.width , self.topMenuView.searchbarBtn.frame.size.height);`
I do this in viewWillLayoutSubviews (have also tried in viewWillAppear) and I call the
[self.topMenuView.searchbarBtn setNeedsDisplay];
but nothings happens.
I
The button's layout properties override your frame settings, since by definition viewWillLayoutSubvies is called before the layout pass. You should just set
self.topMenuView.menuBtn.hidden = YES;
and use autolayout constraints (in the interface builder or in the code) between menuBtn, searchbarBtn and topMenuView to make sure your buttons grow as needed.
As a general rule, we should strive to create interface so that views will correctly position themselves provided their inner state is correctly set and correct constraints are formed, without explicit corrections on our part.

How can I insert a subview below the other subviews

everyone.
I have some labels that I draw them in the xib file, and
add a background view using codes,but the background view
is in the front of these labels, so I cant see them. So,
my question is how can I add a background view below these
labels.
Thank you in advance.
If you Google UIView you'll end up at the documentation - UIView Class Reference
Then you need to look down the list of methods until you find something that sounds like it will roughly fit your needs like these two:
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
and
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
At this point you just have to read the descriptions of how the methods behave and pick whichever one is easiest to implement for your design. It's really good for your own learning if you try to find the answer yourself before you ask a question
You can use the method insertSubview:atIndex:
[self.view insertSubview:backgroundView atIndex:0];
You can call this method in Swift 3
view.insertSubview(yourSubView, at: 0)
or
view.sendSubview(toBack: yourSubView)
In IB the subviews of a view are listed in a hierarchy under that view. Subviews which appear above their siblings in that hierarchy will appear below them in the rendered view. So in the example below, the in 99:99 AM label appears under the Sign In button at runtime.
If your background is just a UIImageView, consider adding the view to your NIB and then setting its UIImage at runtime. That way, you don't get confused. Also, what Paul.s said.
Swift 3 and later.
view.insertSubview(theViewYouWantToAdd, at: 0)
https://medium.com/swift-programming/dynamic-layouts-in-swift-b56cf8049b08
Use VerticalLayout as the parent. it is quite simple and effective

Resources