can't push UINavigationItem on UINavigationBar in Swift4 - ios

We create navigationBar with code and set navigationItem for that navigationBar.
However, pressing navigationItem does not react.
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let navBar: UINavigationBar = {
let navBar = UINavigationBar()
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: navBar.frame.height)
navBar.barTintColor = UIColor.white
let navItem = UINavigationItem()
let saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveBook))
navItem.rightBarButtonItem = saveButton
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel))
navItem.leftBarButtonItem = cancelButton
navItem.rightBarButtonItem?.isEnabled = true
navItem.leftBarButtonItem?.isEnabled = true
navItem.title = "書籍追加"
navBar.pushItem(navItem, animated: true)
return navBar
}()
view.addSubview(navBar)

Clearly this code is not right as the frame is still CGRect.zero. So you are setting the height to 0. You did not get what you want.
not right here:
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: navBar.frame.height)
If you add the code, you will know what I mean as you cannot see the navBar?:
navBar.clipsToBounds = true
view.addSubview(navBar)
you may try:
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: 44)

Related

Adjust position of custom Navigation Back Button

I am using a custom image as the back button of my Navigation Controller but the problem is that the image is not aligned correctly with the title and the right button item. I've been trying to move the back button down a few pixels with no success.
extension UINavigationController {
func addBackButton() {
let imgBack = UIImage(named: "ic_back")
navigationBar.backIndicatorImage = imgBack
navigationBar.backIndicatorTransitionMaskImage = imgBack
navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "",
style: .plain,
target: self,
action: nil)
}
}
This is what it looks like now:
As you can see I need to move the back button down a little, any help would be much appreciated.
This is my code for a custom back button. I added on viewDidLoad() with the image below. Maybe you need to test with your image how to size it correctly. And you can remove the part with the label.
let backButtonView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 44))
let imageView = UIImageView(image: UIImage(named: "back-arrow"))
imageView.frame = CGRect(x: -5, y: 11, width: 12, height: 22)
imageView.image = imageView.image!.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .blue
let label = UILabel(frame: CGRect(x: 10, y: 0, width: 40, height: 44))
label.textColor = .blue
label.text = "Back"
backButtonView.addSubview(imageView)
backButtonView.addSubview(label)
backButtonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backButtonClicked)))
let barButton = UIBarButtonItem(customView: backButtonView)
navigationItem.leftBarButtonItem = barButton

UINavigationItem not displaying left and right barButtons

I'm testing my app on an iPhone 6+. I have a navigation bar which includes two labels, a left bar button (not shown initially), and a right bar button. The problem is that the labels are shown but no matter how hard I try I can't make it show the buttons. They work(if you tap on where they should be, they work as expected) but are not shown. The tests on an iPhone 5s (physic) and iPhone X (simulator) went correctly and the buttons are shown.
Is there any problem with my code?
Thank you.
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
loadWeb()
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.action, target: self, action: #selector(share))
button.tintColor = UIColor.white
self.navigationItem.rightBarButtonItem = button
let frameLabelURLTitle = CGRect.init(x: 25, y: 2, width: (self.navigationController?.navigationBar.frame.size.width)! - 182, height: (self.navigationController?.navigationBar.frame.size.height)! - 20)
let frameLabelURL = CGRect.init(x: 25, y: 2 + frameLabelURLTitle.height, width: (self.navigationController?.navigationBar.frame.size.width)! - 182, height: 10)
let viewLabel = UIView.init(frame: frameLabelURLTitle)
labelTitleURL = UILabel.init(frame: frameLabelURLTitle)
labelURL = UILabel.init(frame: frameLabelURL)
labelURL.textColor = UIColor.white
labelTitleURL.textColor = UIColor.white
labelURL.font = UIFont.systemFont(ofSize: 13.0)
viewLabel.addSubview(labelTitleURL)
viewLabel.addSubview(labelURL)
self.navigationController?.navigationBar.addSubview(viewLabel)
loadingLabel.text = "loadingWeb".localized()
loadingLabel.sizeToFit()
self.navigationController?.isNavigationBarHidden = true
self.placeHolderView.layer.insertSublayer(initGradient(bounds: self.view.bounds, isHorizontal: false), at: 0)
self.loadingGifImageView.image = UIImage.gif(asset: "01-GIF_LOGO")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
self.placeHolderView.isHidden = true
self.navigationController?.isNavigationBarHidden = false
labelTitleURL.text = webView.stringByEvaluatingJavaScript(from: "document.title")
labelURL.text = webView.request?.url?.absoluteString.components(separatedBy: "/")[2]
self.navigationItem.leftBarButtonItem = nil
if !(webView.request?.url?.absoluteString.contains("/blog/"))! {
let newBackButton = UIBarButtonItem(image: UIImage(named: "bt_close")?.withRenderingMode(.alwaysOriginal), style: UIBarButtonItemStyle.plain, target: self, action: #selector(back))
newBackButton.isEnabled = true
print(newBackButton.style)
self.navigationItem.setLeftBarButton(newBackButton, animated: true)
self.navigationItem.leftBarButtonItem = newBackButton
self.navigationItem.backBarButtonItem = newBackButton
}
}

RightBarButtonItems won't be shown anymore on iOS 11

The function self.navigationItem.rightBarButtonItem = cartBarButton won't show my item on the right of UINavigationController's NavBar
EDIT
let cartOriginalImage = UIImage.cart
let cartButton = UIImageButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
cartButton.setBackgroundImage(cartOriginalImage, for: .normal)
cartButton.tintColor = Colors.Navigation.Tint.default
cartButton.addTarget(self, action: #selector(MainViewController.cartButtonPressedInHomePage(sender:)), for: .touchUpInside)
let cartBarButton = UIBarButtonItem(customView: cartButton)
if let badge = badge {
cartBarButton.badgeValue = badge
cartBarButton.badge.backgroundColor = Colors.Navigation.Background.badge
cartBarButton.badge.textColor = Colors.Navigation.Text.badge
}
I had the similar issue with custom image view. My suggestion is to wrap your custom view into container view.
private func configureUserAvatarView() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(avatarViewAction(_:)))
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
customView.addGestureRecognizer(tapRecognizer)
let imageView = FUIImageView(image: #imageLiteral(resourceName: "avatar_default_30х30"))
imageView.isCircular = true
imageView.contentMode = .scaleAspectFit
imageView.frame = customView.bounds
customView.addSubview(imageView)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: customView)
}

Xcode 7.3.1 : Set background image to UINavigationBar and display back button

I want to set logo of app as background image to UINavigationBar and when user traverse into app it should display logo as well as back button on top of it.
Below is code that I've used :
func setNavigationBar() {
let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
let screenSize: CGRect = UIScreen.mainScreen().bounds
let objCustomView = CustomView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: navigationBarHeight))
let objWindow = UIApplication.sharedApplication().keyWindow
objWindow?.addSubview(objCustomView)
self.navigationItem.setHidesBackButton(false, animated:true);
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)
}
The issue with this is that back button goes behind the image.
How to fix this?
After refering post by #NDoc I'm getting extra space in left. Why so?
Also, the back button should be white with no back text i.e. only < arrow.
Below is code for customView :
class CustomView: UIView {
var imgLogo = UIImageView(frame:CGRectZero)
override init(frame: CGRect) {
super.init(frame: frame)
let screenSize: CGRect = UIScreen.mainScreen().bounds
imgLogo.frame = CGRectMake(0, 0, screenSize.width, 44.0)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
imgLogo.image = UIImage(named:"BoM_Logo")
self.addSubview(imgLogo)
}
}
You can display your logo in leftBarButtonItem and set the leftItemsSupplementBackButton to true to display backButton also like this.
let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let item = UIBarButtonItem(customView: logoView)
self.navigationItem.leftBarButtonItem = item
To show the back button with your logo image set leftItemsSupplementBackButton to true
self.navigationItem.leftItemsSupplementBackButton = true
Edit:
If you want custom arrow then you need to use leftBarButtonItems and pass array of BarButtonItem and no need to set leftItemsSupplementBackButton to true like this.
let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let logoItem = UIBarButtonItem(customView: logoView)
let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnBack.setImage(UIImage(named: "Back_Arrow"), forState: .Normal)
btnBack.addTarget(self, action: #selector(self.buttonAction(_:)), forControlEvents: .TouchUpInside)
let backItem = UIBarButtonItem(customView: btnBack)
self.navigationItem.leftBarButtonItems = [backItem, logoItem]
Note: Don't forgot to add buttonAction action method inside your viewController.
Try this in your appDelegate
let image = UIImage.init(named:"upper-bar.png")
UINavigationBar.appearance().setBackgroundImage(image,forBarMetrics:UIBarMetrics.Default)
for back button try this in the viewDidLoad() of your viewController
let image1 = UIImage(named: "go10.png") as UIImage?
let btnLeft = UIButton(type: .Custom)
btnLeft.frame = CGRectMake(0, 0, 25, 25)
btnLeft.setImage(image1,forState:UIControlState.Normal)
btnLeft.addTarget(self, action:(#selector(NameofyourViewController.backBtn(_:))),forControlEvents:UIControlEvents.TouchUpInside)
let leftBarButton = UIBarButtonItem(customView: btnLeft)
self.navigationItem.leftBarButtonItem = leftBarButton
#IBAction func backBtn(sender: UIButton)
{
self.navigationController?.popViewControllerAnimated(true)
}

modal segue slow first time when triggered

I've created a modal segue to a viewController, however when i trigger this segue, there seem to be some delay when viewing the viewController. All i have in the viewController is in the viewDidLoad. what could course this slow modal segue?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Caption".uppercaseString
self.view.backgroundColor = UIColor(hexString: "#0B1E30")
self.navigationController?.navigationBar.barTintColor = UIColor(gradientStyle: UIGradientStyle.LeftToRight, withFrame: CGRectMake(0, 0, self.view.frame.width, (self.navigationController?.navigationBar.frame.height)! + 20), andColors: [UIColor(hexString: "#7F5CE6"), UIColor(hexString: "#9D8FE2")])
let closeButton = UIBarButtonItem(image: UIImage(named: "Cross"), style: UIBarButtonItemStyle.Done, target: self, action: "dismissController")
self.navigationItem.leftBarButtonItem = closeButton
textView?.text = "Add caption (optional)"
textView?.becomeFirstResponder()
textView?.keyboardAppearance = UIKeyboardAppearance.Dark
textView?.font = UIFont(name: "Montserrat-Light", size: 13)
textView?.textColor = UIColor(hexString: "#363636")
capturedImageView = UIImageView(image: capturedPhoto)
capturedImageView?.frame = CGRectMake(8,10, 85, 85)
let path = UIBezierPath(rect: CGRectMake(8, 10, 85, 85))
textView?.textContainer.exclusionPaths = [path]
textView?.addSubview(capturedImageView!)
facebookButton?.tag = 1
facebookButton!.adjustsImageWhenHighlighted = false
facebookButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
facebookButton?.tintColor = UIColor.whiteColor()
facebookButton?.backgroundColor = UIColor(hexString: "#395798")
facebookButton?.contentMode = UIViewContentMode.Center
facebookButton?.setTitle("", forState: UIControlState.Normal)
let fBorder = CALayer()
let width = CGFloat(1.0)
fBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
fBorder.frame = CGRect(x: 0, y: facebookButton!.frame.size.height - width, width: facebookButton!.frame.size.width, height: facebookButton!.frame.size.height)
fBorder.borderWidth = width
facebookButton!.layer.addSublayer(fBorder)
facebookButton!.layer.masksToBounds = true
twitterButton?.tag = 2
twitterButton!.adjustsImageWhenHighlighted = false
twitterButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
twitterButton?.contentMode = UIViewContentMode.Center
twitterButton?.tintColor = UIColor.whiteColor()
twitterButton?.setTitle("", forState: UIControlState.Normal)
let tBorder = CALayer()
tBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tBorder.frame = CGRect(x: 0, y: twitterButton!.frame.size.height - width, width: twitterButton!.frame.size.width, height: twitterButton!.frame.size.height)
tBorder.borderWidth = width
twitterButton!.layer.addSublayer(tBorder)
twitterButton!.layer.masksToBounds = true
let tRightBorder = CALayer()
tRightBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tRightBorder.frame = CGRect(x: 0, y: 0, width: width, height: twitterButton!.frame.size.height)
tRightBorder.borderWidth = width
twitterButton!.layer.addSublayer(tRightBorder)
instagramButton?.tag = 3
instagramButton!.adjustsImageWhenHighlighted = false
instagramButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
instagramButton?.contentMode = UIViewContentMode.Center
instagramButton?.tintColor = UIColor.whiteColor()
instagramButton?.setTitle("", forState: UIControlState.Normal)
let iBorder = CALayer()
iBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iBorder.frame = CGRect(x: 0, y: instagramButton!.frame.size.height - width, width: instagramButton!.frame.size.width, height: instagramButton!.frame.size.height)
iBorder.borderWidth = width
instagramButton!.layer.addSublayer(iBorder)
instagramButton!.layer.masksToBounds = true
let iLeftBorder = CALayer()
iLeftBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iLeftBorder.frame = CGRect(x: 0, y: 0, width: width, height: instagramButton!.frame.size.height)
iLeftBorder.borderWidth = width
instagramButton!.layer.addSublayer(iLeftBorder)
}
This came as a surprise for me but apparently there is a performance issue with textView's property "selectable" when set to true by default (checked on Storyboard).
Try going to your Storyboard, select each textView and uncheck the box "selectable". If you need the textView to be selectable, then just set selectable to true programatically on your viewDidLoad.

Resources