How do i set the back button x position from the starting point of device position? - ios

How do i set the back button x position from the starting point of device position ?
static func backButton(navigationItem: UINavigationItem) {
//Back Button
let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 22))
btnBack.setImage(UIImage(named:"icn-back"), for: .normal)
btnBack.addTarget(self, action: #selector(NavBar.backBtn), for: .touchUpInside)
let leftBtn = UIBarButtonItem.init(customView: btnBack)
leftBtn.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setLeftBarButton(leftBtn, animated: false)
}

let leftView = UIView(frame: CGRect(x: desiredXValue, y: 0, width: 90, height: 21))
let leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 10))
leftButton.setImage(UIImage(named: "Group.png"), for: UIControlState())
leftButton.addTarget(self, action: #selector(ViewController.action(_:)), for: UIControlEvents.touchUpInside)
leftButton.contentMode = .scaleAspectFit
leftView.addSubview(leftButton)
leftView.contentMode = .scaleAspectFit
let leftBarButton = UIBarButtonItem(customView: leftButton)
self.navItem.leftBarButtonItem = leftBarButton
UserDefaults.standard.synchronize()

Related

UIBarButtonItem size displays wrong

I have custom UINavigationBar(for JSQMessagesViewController) with some items. But I have some problems with leftBarButtonItem
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 28, width: screenSize.width, height: 78))
let navItem = UINavigationItem(title: chatName ?? "chat")
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 78, height: 78)
menuBtn.setImage(UIImage(named: "backButton"), for: .normal)
menuBtn.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
let menuBarItem = UIBarButtonItem(customView: menuBtn)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 78)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true
navItem.leftBarButtonItem = menuBarItem
navBar.titleTextAttributes = [.font : UIFont(name: "Acrom-Bold", size: 26)!, .foregroundColor : UIColor.white]
navBar.setItems([navItem], animated: false)
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.3)
navBar.isTranslucent = true
self.view.addSubview(navBar)
}
But my BackButton looks wrong.
What is the problem with the height of the button?
If a bar button item has a custom view, you must size the width of the custom view using internal autolayout constraints.
But you must not size the height larger than the runtime likes, as you are doing. Delete these lines:
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true

Setup rightBarButtonItem in navigationBar

I have stuck on this bug for days. I have a problem with rightBarButtonItem. I want to create a custom view. Inside custom view, there are 2 items: label, imageView. When I type this:
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .black
customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)
let coinLabelNavigationBar = UILabel()
coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
coinLabelNavigationBar.text = "200"
coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
coinLabelNavigationBar.textColor = UIColor.white
let coinImage = UIImageView()
coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
coinImage.translatesAutoresizingMaskIntoConstraints = false
coinImage.image = UIImage(named: "Coin")
customView.addSubview(coinLabelNavigationBar)
customView.addSubview(coinImage)
let rightBarButton = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = rightBarButton
It doesn't work, navigationBar shows me this:
Do you have any idea, how to fix it?
Thanks for your answers.
try this -
let customView = UIView()
// customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .black
customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)
let coinLabelNavigationBar = UILabel()
coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
// coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
coinLabelNavigationBar.text = "200"
coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
coinLabelNavigationBar.textColor = UIColor.white
let coinImage = UIImageView()
coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
// coinImage.translatesAutoresizingMaskIntoConstraints = false
coinImage.image = UIImage(named: "coin.jpg")
customView.addSubview(coinLabelNavigationBar)
customView.addSubview(coinImage)
let rightBarButton = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = rightBarButton
For Swift 3.0:
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)

Two UIButton form one having gradient color and another having border and rounded corner

I am trying to achive this using gradient color and corner radius on [.topLeft,.bottomLeft] and [.topRight,.bottomRight] but I am not able to achieve the border color on one side.
Here is my code
//btnMale gradient color
btnMale.roundCorners([.topLeft,.bottomLeft], radius: 20)
btnFemale.roundCorners([.topRight,.bottomRight], radius: 20)
btnMale.backgroundColor = .clear
let gradient1: CAGradientLayer = CAGradientLayer()
gradient1.frame = CGRect(x: 0, y: 0, width: (self.view.frame.size.width - 90)/2 , height: btnMale.frame.size.height)
gradient1.colors = [Colors.appYellow,Colors.appRed].map { $0.cgColor }
gradient1.startPoint = GradientOrientation.horizontal.startPoint
gradient1.endPoint = GradientOrientation.horizontal.endPoint
btnMale.layer.insertSublayer(gradient1, at: 0)
btnMale.applyGradient(withColours: [Colors.appYellow,Colors.appRed], gradientOrientation: .horizontal)
btnMale.borderWidth = 0.0
btnFemale.borderWidth = 0.5
btnFemale.borderColor = .black
btnMale.setImage(UIImage(named: Images.imgMaleIconWhite), for: .normal)
btnFemale.setImage(UIImage(named: Images.imgFemaleIconBlack), for: .normal)
and here is my output
Please help.
Pradip it's better to add buttons in UIView. It will be easy for you to manage. I made UI like you want using UIView. See this,
let button = UIButton.init(frame: CGRect.init(x: 0, y: 250, width: 200, height: 100))
button.setTitle("HI", for: .normal)
button.addTarget(self, action: #selector(self.btnTapped(pageNo:)), for: .touchUpInside)
self.view.addSubview(button)
let view = UIView.init(frame: CGRect.init(x: 50, y: 250, width: 250, height: 50))
view.backgroundColor = UIColor.red
self.view.addSubview(view)
let btnMale = UIButton.init(type: .custom)
btnMale.frame = CGRect.init(x: 0, y: 0, width: 125, height: 50)
btnMale.setTitle("Male", for: .normal)
btnMale.applyGradient(colours: [UIColor.yellow,UIColor.red])
view.addSubview(btnMale)
let btnFemale = UIButton.init(type: .custom)
btnFemale.frame = CGRect.init(x: 125, y: 0, width: 125, height: 50)
btnFemale.setTitle("Female", for: .normal)
view.addSubview(btnFemale)
view.layer.cornerRadius = 25
view.clipsToBounds = true
Output of above code will look like this.

How to add a static header in JSQMessageView Controller

How i can add header just like the image below which should not scroll up with view controller.
To add static header view, use following function (Swift 4):
Note: I have tried to put this kind of header view directly using storyboard. But, it wont worked. So, finally I achieved it by adding programatically.
func addHeaderView() {
let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
selectableView.addSubview(btn)
let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
titleLabel.textColor = UIColor.white
titleLabel.text = "Winter event 2017"
selectableView.addSubview(titleLabel)
let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
subTitleLabel.textColor = UIColor.white
subTitleLabel.text = "135 members"
selectableView.addSubview(subTitleLabel)
let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnOptions.tag = 2
selectableView.addSubview(btnOptions)
view.addSubview(selectableView)
}
//then make a action method :
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
self.navigationController?.popViewController(animated: true)
} else {
askForSettings()
}
}
Also put this line of code in viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)

Bound the button to top right corner of the view iOS swift

I have a view in which i have multiple labels and a button i want the button to be bound to the top right corner
when i apply the add Missing constraints layout the button gets separate position for different devices Please help me to resolve this
NEVER use add missing constraints. This is a bad way of constructing your layout.
The easiest way to get your desired look is to decide on the size you want your button to be (eg. 30x30)
Then you add the following constraints to your button:
This sets the button's width & Height:
Then pin it to the right top corner:
//Just in case if anybody needs it with code:
//I have added 4 buttons to a custom view's each corner:
var customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
customView.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
customView.backgroundColor = UIColor.red //give color to the view
customView.center = self.view.center
let crossButton = UIButton(frame: CGRect(x: -10, y: -10, width: 30, height: 30))
crossButton.layer.cornerRadius = 15
crossButton.backgroundColor = UIColor.black
crossButton.setImage(UIImage(named: "cross.png"), for: .normal)
crossButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
crossButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let flipButton = UIButton(frame: CGRect(x: customView.frame.width-15, y: -10, width: 30, height: 30))
flipButton.layer.cornerRadius = 15
flipButton.backgroundColor = UIColor.black
flipButton.setImage(UIImage(named: "done.png"), for: .normal)
flipButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
flipButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let scaleButton = UIButton(frame: CGRect(x: -10, y: customView.frame.height-20, width: 30, height: 30))
scaleButton.layer.cornerRadius = 15
scaleButton.backgroundColor = UIColor.black
scaleButton.setImage(UIImage(named: "done.png"), for: .normal)
scaleButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
scaleButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let editButton = UIButton(frame: CGRect(x: customView.frame.width-20, y: customView.frame.height-20, width: 30, height: 30))
editButton.layer.cornerRadius = 15
editButton.backgroundColor = UIColor.black
editButton.setImage(UIImage(named: "done.png"), for: .normal)
editButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
editButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
customView.addSubview(crossButton)
customView.addSubview(flipButton)
customView.addSubview(scaleButton)
customView.addSubview(editButton)
self.view.addSubview(customView)
}
func crossButtonTapped(_ sender:UIButton) {
print("Cross Button was tapped")
}

Resources