Swift Center Circle Button in TabbarViewController iOS - ios

My scenario, I am trying to create centre circle button in UITabbarViewController. I tried many sample but not getting output like below Image. Please provide some code to achieve this. I am using storyboard for this.
sample Image

You need to do is your UITabbarViewController must contain five tabs in bottom tab bar that you can do by adding five view controller and make sure the middle tab contain no title and icon.
After that in onCreate method of your UITabbarViewController add this custom button Code -
let button = UIButton(type: .custom)
var toMakeButtonUp = 40
button.frame = CGRect(x: 0.0, y: 0.0, width: 65, height: 65)
button.setBackgroundImage(ADD, for: .normal)
button.setBackgroundImage(ADD, for: .highlighted)
let heightDifference: CGFloat = CGFloat(toMakeButtonUp)
if heightDifference < 0 {
button.center = tabBar.center
} else {
var center: CGPoint = tabBar.center
center.y = center.y - heightDifference / 2.0
button.center = center
}
button.addTarget(self, action: #selector(btnTouched), for:.touchUpInside)
view.addSubview(button)
FYI - toMakeButton is the margin from bottom you can set it accordingly.
and #selector(btnTouched) is the function defined in class which will perform on click on the button.

Related

Xcode horizontal UIScrollView

I have 4 buttons in a UIViewController in XCode. I already added code to them so I would prefer to keep the buttons. I want to have the buttons side by side in the view where there is only one button on the screen at a time and the edge of the other buttons on the edge of the screen. Where you can just barely see them. I wanted to know how would I be able to accomplish this? If you need more information than this just let me know?
var scrollView: UIScrollView!
private func addButton() {
for index in 0...2{
let button = UIButton()
let x = scrollView.frame.size.width * CGFloat(index)
button.frame = CGRect(x: x, y: 0, width: scrollView.frame.width, height: self.view.frame.height)
button.backgroundColor = UIImage(named: ColorArr[index])
scrollView.contentSize.width = scrollView.frame.size.width * CGFloat(index + 1)
scrollView.addSubview(button)
}
}
You can call this function in viewDidLoad() and i took color array to differentiate buttons

How can I properly place a UIButton on top of a loaded SFSafariViewController?

I have read many articles about the SFSafariViewController and I believe that it offers splendid functionality in iOS apps. However, when I load my SFSafariViewController, I intentionally hide the navigation bar because I want one custom fixed button in the upper left corner to dismiss the view controller.
override func viewDidAppear(_ animated: Bool) {
let safariViewController = PSSafariViewController(url: URL(string: blogUrl)!, entersReaderIfAvailable: true)
present(safariViewController, animated: false) {
var frame = safariViewController.view.frame
let OffsetY: CGFloat = 44
frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
safariViewController.view.frame = frame
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(PSBlogViewController.buttonAction), for: UIControlEvents.touchUpInside)
btn.tag = 1 // change tag property
btn.isOpaque = true
safariViewController.view.addSubview(btn)
safariViewController.view.bringSubview(toFront: btn)
print(btn.description)
}
}
As you can see, I alter the frame so that the bar at the top is not visible. That code runs fine. But when I try to add a UIButton, it appears briefly and then is covered when I run the app. It's a simple blog reader app that uses the SFSafariViewController. Maybe Apple doesn't want developers running around messing with this, but any solutions or workarounds to make the button stay visible are greatly appreciated!
Here's the info about the button: 0x7f950b618db0; frame = (100 400; 100 50); tag = 1; layer = CALayer: 0x60000023ab60
Why don't you use one of the other WebView classes to get the extra functionalities you desire?
5.1.1 (iv) SafariViewContoller must be used to visibly present information to users; the controller may not be hidden or obscured by
other views or layers. Additionally, an app may not use
SafariViewController to track users without their knowledge and
consent.
It is definitely true that Apple won't want the SFSafariViewController to be obscured. However, I did figure out that when I present it, in the completion block I can add a button and what was causing trouble was that I had to increase the zPosition of the layer of the button's view like so:
present(safariViewController, animated: false) {
var frame = safariViewController.view.frame
let OffsetY: CGFloat = 44
frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
safariViewController.view.frame = frame
self.btn = UIButton(frame: CGRect(x: 5, y: 50, width: 50, height: 50))
self.btn.layer.cornerRadius = 25
self.btn.backgroundColor = UIColor(red: 0, green: 170/255, blue: 240/255, alpha: 0.5)
self.btn.setTitle("←", for: .normal)
self.btn.addTarget(self, action: #selector(safariViewController.buttonAction(sender:)), for: .touchUpInside)
self.btn.tag = 1 // change tag property
self.btn.isOpaque = true
safariViewController.view.addSubview(self.btn)
safariViewController.view.bringSubview(toFront: self.btn)
self.btn.layer.zPosition = safariViewController.view.layer.zPosition + 1
for subview in safariViewController.view.subviews {
subview.isUserInteractionEnabled = false
}
self.btn.isEnabled = true
self.btn.isUserInteractionEnabled = true
//print(self.btn.description)
}

passing Events from UIButton to TabBarItem

i wanted to make my UITabBarItem's size Bigger then other buttons so i tried this in Subclass of TabBarController :
var button = UIButton(type: .Custom)
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height)
button.setBackgroundImage(buttonImage, forState: .Normal)
button.setBackgroundImage(highlightImage, forState: .Highlighted)
var heightDifference: CGFloat = buttonImage.size.height - self.tabBar.frame.size.height
if heightDifference < 0 {
button.center = self.tabBar.center
}
else {
var center = self.tabBar.center
center.y = center.y - heightDifference / 2.0
button.center = center
}
self.view.addSubview(button)
it worked fine cause now i have a Button on top of my BarButtonItem (expected Behavior ) but now this new button is blocking the TouchEvents which supposed to handle by barbuttonitem , any idea how can i solve this problem ?? i followed this article for getting this newButton:
what i want is look like this :
SOLVED:
All i needed was to disable the user interaction for my button.
you can try below code for your UIButton Clicked event e.g.
-(void)clickedEvent:(id)sender{
.... some code
[self.tabBarController setSelectedIndex:2];
..... some code
}
Hopefully, It should work.

Custom Class view and selector reference

I'm making a custom class that just has some basic functions I can reuse once the class is initialized in any viewController. The problem is I don't know how to refer to the view or any functions being called in the #selector and I get the error: use of unresolved identifier "view". Anyone know how that's done? The sample below is to create a new UIButton.
import Foundation
import UIKit
class Utils {
var newUpdatesBtn: UIButton!
func setupButtonNewUpdates() {
newUpdatesBtn = UIButton(type: UIButtonType.System) // .System
newUpdatesBtn.setTitle("New Updates", forState: UIControlState.Normal)
newUpdatesBtn.bounds = CGRect(x: 0, y: 0, width: 123, height: 27)
newUpdatesBtn.center = CGPoint(x: view.bounds.width / 2, y: 90) // 80 point button, 20 point status bar, 40 point half button, 8 point margins
newUpdatesBtn.setBackgroundImage(UIImage(named: "new-updates"), forState: UIControlState.Normal)
newUpdatesBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
newUpdatesBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
newUpdatesBtn.addTarget(self, action: #selector(newUpdatesButtonPressed), forControlEvents: UIControlEvents.TouchUpInside)
self.newUpdatesBtn.alpha = 0;
self.navigationController!.view.addSubview(newUpdatesBtn)
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.newUpdatesBtn.alpha = 1;
})
}
}
(problem lines)
view.bounds.width
newUpdatesBtn.addTarget(self, action: #selector(newUpdatesButtonPressed), forControlEvents: UIControlEvents.TouchUpInside)
This is because you have not set any reference in view property.
You can do like this:
func setupButtonNewUpdates(targetView:UIView)
{
newUpdatesBtn = UIButton(type: UIButtonType.System) // .System
.
.
.
newUpdatesBtn.center = CGPoint(x: targetView.bounds.width / 2, y: 90) // 80 point button, 20 point status bar, 40 point half button, 8 point margins
.
.
.
targetView.addSubView(newUpdatesBtn);
}
Now whenever you call this method from any viewcontroller just pass the view of that VC. for example:
var util:Utils = new Utils();
util.setupButtonNewUpdates(self.view);

How do i make a menu like Spotify

Ok as the question suggests, I want to make a menu like the spotify app currently have.
I have come across a code in github that resembles this but i want to know how to achieve this on a DIY(Do it yourself) level.
I can understand that a pageview controller is needed but am lost. Please point me to the right direction.
EDIT
So that people might understand properly the requirement I am talking about, Here is a png
Notice the top menu that slides with the underline as the selected menu.
The github link that i posted is a library that does that, but its in swift, so can anyone tell me as to how to achieve this in objective c
Regards
I have made a similar kind of sample but that is in Swift 2.0. Have a look. May be it can be of some help.
In the sample link posted below, the query you are asking can be done in the following way.
We can use this method to setup the buttons first on the top of page view
In your terms this is the Top Menu
func setupSegmentButtons(){
navigationView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, (self.navigationController?.navigationBar.frame.size.height)! - 4))
//navigationView.backgroundColor = UIColor.redColor()
navigationView.layoutIfNeeded()
let numControllers:NSInteger = viewControllerArray.count
if(buttonText == nil)
{
buttonText = NSArray(objects: "First","Second","Third")
}
let buttonWidth:CGFloat = self.view.frame.size.width/CGFloat(numControllers)
for (var i = 0; i < numControllers; i++) {
let button:UIButton = UIButton(frame: CGRectMake( CGFloat(i) * buttonWidth, 0, buttonWidth, 40))
button.addTarget(self, action: "tapSegmentButtonAction:", forControlEvents: .TouchUpInside)
navigationView.addSubview(button)
button.tag = i
button.titleLabel?.font = UIFont(name: "Helvetica", size: 14)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.setTitle(buttonText.objectAtIndex(i) as? String, forState: .Normal)
}
self.view.addSubview(navigationView)
self.setupSelector()
}
Now we have to add the selection bar which will slide accordingly with the menu selected.
Setting up the selection bar view
func setupSelector(){
//selection bar view
selectionBar = UIView(frame: CGRectMake(X_BUFFER - X_OFFSET + SELECTED_POSITION , SELECTOR_Y_BUFFER, (self.view.frame.size.width - 2 * X_BUFFER)/CGFloat(viewControllerArray.count), SELECTOR_HEIGHT))
selectionBar.backgroundColor = UIColor.brownColor()
selectionBar.alpha = 0.8
navigationView.addSubview(selectionBar)
}
Now we can use the scroll view's scrollViewDidScroll delegate in order to make the selection bar view slides towards the selected menu. Each time this delegate is called whenever the page view controller is scrolled, we change the frame of selected bar view every moment.
func scrollViewDidScroll(scrollView: UIScrollView) {
let xFromCenter : CGFloat = self.view.frame.size.width-scrollView.contentOffset.x;
let xCoors : CGFloat = X_BUFFER + selectionBar.frame.size.width * CGFloat(currentPageIndex) - X_OFFSET
_ = selectionBar.frame
if(CGFloat(xCoors)-xFromCenter / CGFloat(viewControllerArray.count) > self.view.frame.size.width - selectionBar.frame.width)
{
selectionBar.frame = CGRectMake(self.view.frame.size.width - selectionBar.frame.width, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
else if(CGFloat(xCoors)-xFromCenter / CGFloat(viewControllerArray.count) < 0)
{
selectionBar.frame = CGRectMake(0, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
else
{
selectionBar.frame = CGRectMake(CGFloat(xCoors)-xFromCenter/CGFloat(viewControllerArray.count), selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
}
For more details please see the sample link below.
https://github.com/RajanMaheshwari/PageViewController

Resources