Adding view from xib in front of other views - ios

I'm trying to add a custom view from a xib in front of other views. However, for some reason it's never placed as the front view when theres a UITableView on screen. Here's my code:
noInternetView = NSBundle.mainBundle().loadNibNamed("NoInternetView", owner: self, options: nil)[0] as! NoInternetView
noInternetView.translatesAutoresizingMaskIntoConstraints = false
noInternetView.delegate = self
self.view.addSubview(noInternetView)
self.view.addConstraint(NSLayoutConstraint(item: noInternetView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: noInternetView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0))
I place this at the bottom of my viewDidLoad() code. Why could this be?

noInternetView.hidden = true
your view is hidden, do it visible
noInternetView.hidden = false

self.view.bringSubviewToFront(noInternetView)
Try this it may help you.

First
Check is there a noInternetView have width and height constraints
Second
Add self.view.layoutIfNeeded() after self.view.addConstraint
this help to adjust autolayout you added

If you want your view to be on the top, add it to the window instead.

Related

Wrong NSLayoutConstraint Causes Black Screen

I want to place header view on top of screen with NSLayoutConstraint (I must use NSLayoutConstraint). When I do it like in below code, view places corruptly in somewhere else and also controllers background color turns black and nothing works. Where am I doing wrong?
I searched below posts for not opening a duplicate post but nothing fixed it:
Programmatically creating constraints bound to view controller margins
Programmatically Add CenterX/CenterY Constraints
EDIT: This controller is inside navigation controller but I'm not sure If It is related.
override func viewDidLoad(){
self.view.backgroundColor = UIColor.white
boxView.backgroundColor = Color.Common.welcomeScreenBackgroundColor.withAlphaComponent(0.5)
boxView.translatesAutoresizingMaskIntoConstraints = false
self.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubView(boxView)
}
override func viewDidLayoutSubviews() {
//Header = 20 from left edge of screen
let cn1 = NSLayoutConstraint(item: boxView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0)
//Header view trailing end is 20 px from right edge of the screen
let cn2 = NSLayoutConstraint(item: boxView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: 0)
//Header view height = constant 240
let cn3 = NSLayoutConstraint(item: boxView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant:240)
//Header view vertical padding from the top edge of the screen = 20
let cn5 = NSLayoutConstraint(item: boxView, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0)
self.view.addConstraints([cn1,cn2,cn3,cn5])
}
The problem was setting translatesAutoresizingMaskIntoConstraints to false on Superview. So I deleted the;
self.view.translatesAutoresizingMaskIntoConstraints = false
and this solves the problem. I think this causes app creates constraint for superview.

Center the subview in UIView using constraints in IOS

i have three subview like subview1,subview2,subview3 in a UIViewController inside a UIView.
I'm using constraints on them, the constraints which i'm using to show then in a center when the button is clicked are these,
Horizantally in Container,
Vertically in Container,
width and
height.
Issue :
When I run the app and click the button once it comes in the right position. But when i dismiss it and try again to open it, the subview change its position itself and comes on the top left of the UIViewcontroller ,
As you can see in a screen shot, but i want to show the view every time when i click the button in the center of the UIViewcontroller.
I'm confused that constraints are right but why it always changes its position when we open it again.
My code is ,
- (IBAction)Precord:(id)sender {
_DateView.hidden=NO;
[self.DateView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
The contents in story board are like this ,
enter image description here
It depends on the way you are dismissing the View. I just tried below code and it worked for me
- (IBAction)btn_Tapped:(id)sender {
_smallView.hidden = false;
[_smallView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
- (IBAction)hide_Tapped:(id)sender {
_smallView.hidden = true;
}
Basically i am just hiding the view. And when btn_Tapped, everytime the view is displaying with bouncing animation in the center. Let me know how you go!
try this
yourSubView.center = CGPointMake(yourMainView.frame.size.width / 2,
yourMainView.frame.size.height / 2);
Simple way to programmatically add constrains. Modify it with your desire size.
Notice 1: toItem: self, where self is just a view where u added the subView.
Notice 2: SubView MUST be added to self or a subView of self.
subView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: subView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0.5, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0).isActive = true
I would recommend adding constrains in Storyboard. Easier to see/read, clear code and error validator.
On your problem i would say it a animation problem. Just before adding your view run again the method that set the view frames/constrains (to reset the animation position modification).
Or just when animation is finish, instead of hiding the view, remove it from super view. And when u need it again, just add it again.

Tab Bar Height and button size - Fixed?

i have a custom tab bar controller, that i want to add 2 buttons to. 1 enlarged centre button and 1 button on the left to create a side out burger menu that is launched from the tab bar instead instead of the top navigation bar.
i was going to try and get the tab bar height programatically so i can set the button heights ect from that. so i read up and tried the following code which does not work.
self.tabBarController?.tabBar.frame.size.height
i read somewhere else that the tabbar is always a fixed 49 pixels regardless of device?
if that is the case is it safe to use something like:
menuButtonFrame.origin.y = self.view.bounds.height - (CGFloat(49) - menuButtonFrame.size.height) / 2
to set the position of my button? (the black box) its not positions right just yet
also wondering what the default value for the tabbar button is?
Create a UIView Like this and set the height of the center item as your wish.
And then in TabbarView Controller. add this view to the tabbar View Like this.
UITabBar.appearance().shadowImage = UIImage()
customNavBar = NSBundle.mainBundle().loadNibNamed("CustomTabBarView", owner: self, options: nil)[0] as! UIView
bdNavBar.translatesAutoresizingMaskIntoConstraints = false
self.tabBar.addSubview(customNavBar)
And then add Constraints to the custom Tabbar.
self.view.addConstraint(NSLayoutConstraint(item: customNavBar, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: customNavBar, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: customNavBar, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0))
bdNavBar.addConstraint(NSLayoutConstraint(item: customNavBar, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
self.tabBar.bringSubviewToFront(customNavBar)

How to add a view between table view and navigation bar?

So what i m trying to achieve is I want to add a view between a tableview and the navigation bar. The hierarchy would be like:
Top-NavigationBar-betweenView-tableView-Bottom.
I have tried something like this, but the betweenView does not go under the navigation bar, and the tableView has a weird blank space above it. Anyone have any idea? Thank you!
let betweenView = searchController.searchBar
self.view.addSubview(betweenView)
let upperConstraint = NSLayoutConstraint(item: betweenView, attribute: .top, relatedBy: .equal, toItem: self.tableView, attribute: .top, multiplier: 1, constant: (self.navigationController?.navigationBar.frame.height)!)
let lowerConstraint = NSLayoutConstraint(item: self.tableView, attribute: .top, relatedBy: .equal, toItem: searchView, attribute: .bottom, multiplier: 1, constant: 0)
self.view.addConstraint(upperConstraint)
self.view.addConstraint(lowerConstraint)
You'll want to do 2 things here:
1) Layout the constraint for the search view to the top layout guide. This will move your search view below the navigation bar on a per need basis:
let upperConstraint = NSLayoutConstraint(item: searchView, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0)
2) You need to make sure the table does not automatically adjust the scrolling to the navigation controller:
tableView.automaticallyAdjustsScrollViewInsets = false

Adding auto layout constraints between different view controller's view

As an academic exercise for a future UI, I am trying to add constraints between two table views, belonging to two different child view controllers of the root controller. In my RootViewController class below, tvc is displayed as expected in a 400x500 frame, but tvc2 is consuming the entire frame instead of being a 400x500 frame to the right of tvc. Basically, the constraints are apparently being ignored. I'm using an iPad sim in landscape.
override func viewDidLoad() {
super.viewDidLoad()
let v = self.view
v.backgroundColor = UIColor.greenColor()
var tvc :OrderTableViewController = OrderTableViewController(style: UITableViewStyle.Plain)
var tvc2 :OrderTableViewController = OrderTableViewController(style: UITableViewStyle.Plain)
self.addChildViewController(tvc)
self.addChildViewController(tvc2)
v.addSubview(tvc.view)
v.addSubview(tvc2.view)
tvc.didMoveToParentViewController(self)
tvc2.didMoveToParentViewController(self)
//tvc.view.setTranslatesAutoresizingMaskIntoConstraints(false)
//tvc2.view.setTranslatesAutoresizingMaskIntoConstraints(false)
//self.view.setTranslatesAutoresizingMaskIntoConstraints(false)
tvc.view.frame = CGRectMake(0, 0, 400, 500)
self.view.addConstraint(NSLayoutConstraint(
item: tvc2.view,
attribute: .Top,
relatedBy: .Equal,
toItem: tvc.view,
attribute: .Top,
multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(
item: tvc2.view,
attribute: .Bottom,
relatedBy: .Equal,
toItem: tvc.view,
attribute: .Bottom,
multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(
item: tvc2.view,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1, constant: 400))
self.view.addConstraint(NSLayoutConstraint(
item: tvc2.view,
attribute: .Left,
relatedBy: .Equal,
toItem: tvc.view,
attribute: .Right,
multiplier: 1, constant: 0))
}
This line,
tvc2.view.setTranslatesAutoresizingMaskIntoConstraints(false)
should be uncommented. When you add a view programmatically, and you're using constraints, you should always set that to false. On the other hand, you should not set that to false for controller's main view.
Also, the width constraint, since it only applies to tvc2, should be added to tvc2, not to self.view (although it should work either way).
It's also a bit odd, that you're adding one view using frames, and the other with constraints. It would be better to do both using the same paradigm.

Resources