Constrain Button Programmatically To Bottom Center Of Tab Bar - ios

I am building a tab bar with a prominent middle button for adding a post. The issue I'm running into is that my button fits well on iPhone 11 Pro Max but is not positioned correctly on other size iPhones (see images).
I think the issue is that I'm setting the Y position absolutely instead of relative to the tabBar. I am confused on how to do this since I am adding the button programmatically instead of through the storyboard (where I know how to use relative constraints). Here is where I am setting the position:
override func viewDidLayoutSubviews() {
button.frame = CGRect.init(x: self.tabBar.center.x - 32, y: self.view.bounds.height - 115, width: 64, height: 64)
}
How can I set a relative position programmatically for my button so it is always half above and half below the tab bar, regardless of phone size?

try this
let tabBarHeight = 64
let mainButton: UIButton = UIButton(type: .custom)
mainButton.frame = CGRect(origin: CGPoint(x: 0.0, y: win.frame.size.height),size: CGSize(width: tabBarHeight, height: tabBarHeight))
mainButton.center = CGPoint(x: win.center.x, y: win.frame.size.height - tabBar.layer.bounds.height)
you set the size and set the center of the button to the center of the TabBar or move a little up like this code do.

Just add another tabitem in the storyboard builder and then make:
tabbar.clipsToBounds = false
This is the best solution.

Related

iOS SearchController Searchbar does not work after adding constraint programmatically

I have an iOS project whereby the SearchController's SearchBar is added to the view as a subview:
let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))
subView.addSubview((self.searchController.searchBar))
self.view.addSubview(subView)
As shown in the screenshot below, the searchbar is partially obscured by the navigation bar:
As such, I added the following constraint:
subView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([subView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)])
The searchbar is now positioned correctly below the navigation bar as shown:
However, I'm now not able to click onto the searchbar to input any text. How can I resolve this?
How I resolved this at the end.
I still made use of a subView for the searchbar. But this time, instead of giving a fixed value for y, I used the heights of the safeArea and the navigation bar as shown:
let window = UIApplication.shared.windows.first
let safeheight = window?.safeAreaInsets.top
let navHeight = self.navigationController?.navigationBar.frame.height
let subView = UIView(frame: CGRect(x: 0, y: safeheight! + navHeight!, width: 350.0, height: 45.0))
subView.addSubview((self.searchController.searchBar))
self.view.addSubview(subView)
This time, the searchbar was positioned correctly and worked perfectly.

A navigator menu slide in from right

This is code for navigator menu:
let menuIconImageView = UIImageView(image: UIImage(named: "menu_icon"))
menuIconImageView.contentMode = .scaleAspectFit
menuIconImageView.frame = CGRect(x: 35, y: 30, width: 35, height: 30)
menuIconImageView.isUserInteractionEnabled = false
self.view.addSubview(menuIconImageView)
How do I make a menu slide in from right on a click?
Create a width constraint for your menuIconImageViewand set it to 0 for it's initial value. Once you click on the button set the width value to 35 then animate with UIView.animate(withDuration:){ view.layoutIfNeeded() }. Also make sure that you have a trailing constraint to superView and the value is set to 0 for your menuIconImageView.
You can check below thread for code examples
How to animate a UIView with constraints in Swift?

Navigation bar overlapping image after back segue

Navigation bar overlaps the image after user back from segue.
When the view first loaded it looks ok but after performing the segue and come back it looks like this.
here is the code for that image
let logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 67))
let imageView = UIImageView(frame: CGRect(x: 0, y: -30, width: 200, height: 67))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "navbarlogo")
imageView.image = image
logoContainer.addSubview(imageView)
navigationItem.titleView = logoContainer
Honestly, your problem is the fact you are creating an overlap of the ImageView by specifying a -30 y coordinate. It's higher in the z-order (compared to your navigation bar) in one case but lower in the z-order in the segue scenario.
I suspect if you use "Debug View Hierarchy" in XCode in both scenarios, you would be able to see this is what is happening.
I would change your approach as to how the logo container is being laid out

Reproducing Music App's NavigationBar Animation

In my project I want to achieve a very similar animation like in the Artist-ViewController of the stock Music App.
When the UITableView scrolls to a specific offset the navigationBar's tintColor, the color of the title and the color of the statusbar changes.
To achieve that I added an UIView (I named it containerView) as a subView of the UINavigationBar and inside that containerView I added a UIBlurEffect. Now when the tableView scrolls I am listening to scrollViewDidScroll to change the earlier named attributes which works really good.
containerView.frame = CGRect(x: 0, y: -(statusBarHeight), width: UIScreen.main.bounds.width, height: frame.height + statusBarHeight)
containerView.clipsToBounds = true
insertSubview(containerView, at: 0)
let blurEffect = UIBlurEffect(style: .extraLight)
overlayView.effect = blurEffect
overlayView.backgroundColor = unOverlayColor
let height = frame.height + statusBarHeight
overlayView.frame = CGRect(x: 0, y: containerView.frame.height, width: containerView.frame.width, height: height)
containerView.addSubview(overlayView)
My only problem is that the containerView is placed above the UINavigationBar's navigationItems and therefore hides them.
So my question is how can I add the containerView behind the navigationItems to the UINavigationBar?
I figured out that it has to be something with the way how navigationBars are handled in iOS 11 because on iOS 10 everything works fine.

iOS - NavBar logo title is shifting to the left

NavBar logo title is shifting to the left because of rightBarButtonItem. How can I set its X position center horizontally.
Try something like this
if let navBar = self.navigationController?.navigationBar {
let image = UIImageView()
image.image = UIImage(named: "Icon.png")
image.frame = CGRectMake(0, 0, 50, 50)
image.center = CGPoint(x: navBar.center.x, y: navBar.center.y-19)
navBar.addSubview(image)
}
Given that you used Storyboard:
If you view your warnings, Xcode should notify you that the Frame will be different at run time. Often, it can correct that for you automatically if you click on the yellow triangle and select "Update constraints automatically".
Let me know if that helps.

Resources