swift navigation bar title image position - swift

I created a navigation bar title image, it is working well.
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 5, height: 10))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: imageTitle)
imageView.image = image
navigationItem.titleView = imageView
but i added an item button at the right side of navigation bar, the title image move to left a bit. How to make the image title in the center of the screen?

Related

Cant get rid of grey colour behind TabBar in Swift

I put a background image to my view, so the full view should have it.
let background = UIImage(named: "background")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
Also I have a barTintColor for my tabBar:
tabBar.barTintColor = UIColor.blue
The problem is when my TabBar is transparent for some percent, I can see behind the grey color(probably the default color) instead of my view's background image...
Debug view hierarchy:
Try the following
UITabBar.appearance().isTranslucent = false

Navigation Bar logo title not centered

I've implemented a logo as the navigation bar title, using the code below:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "nav_bar_icon_gradient.png")
imageView.image = image
self.navigationItem.titleView = imageView
It works however the item is fully centred on the navigation bar (see screenshot), I'm aware it could be because of the navigation bar item on the right. Does anyone know how to fix this?

Align Drop Down Menu Title to prevent overlapping of other buttons

I have the below image where the title of the drop down menu title is overlapped with other buttons. How to solve this issue?
Code:
func createDropDownMenu() {
// create the drop down menu
let title = prepareNavigationBarMenuTitleView()
prepareNavigationBarMenu(title)
updateMenuContentOffsets()
}
func prepareNavigationBarMenuTitleView() -> String {
// Both title label and image view are fixed horizontally inside title
// view, UIKit is responsible to center title view in the navigation bar.
// We want to ensure the space between title and image remains constant,
// even when title view is moved to remain centered (but never resized).
titleView = DropDownTitleView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
titleView.addTarget(self,
action: #selector(DocumentViewController.willToggleNavigationBarMenu(_:)),
for: .touchUpInside)
titleView.addTarget(self,
action: #selector(DocumentViewController.didToggleNavigationBarMenu(_:)),
for: .valueChanged)
titleView.titleLabel.lineBreakMode = NSLineBreakMode.byClipping
titleView.titleLabel.numberOfLines = 2
titleView.titleLabel.textColor = UIColor.black
titleView.title = currentNode.title
navigationItem.titleView = titleView
return titleView.title!
}
I had to set the frame of the TitleLable and set the numberOfLines = 0 to solve my problem.
Code:
titleView.titleLabel.frame = CGRect(x: 0, y: 0, width: 600, height: 80)
titleView.numberOfLines = 0
titleView.titleLabel.text = currentNode.title

Logo in Navigation Bar Swift not showing

I'm trying to add a logo to my page's navigation bar, however it doesn't appear when I added this code to my viewDidLoad():
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Does anybody know why this might not be working? I checked that the image name is correct. This is my storyboard if it helps:
So it worked when I made an outlet,
#IBOutlet weak var navItem: UINavigationItem!
then put the following code in my viewDidAppear:
let logo = UIImage(named: "Dog_Log_Icon_4"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
navItem.titleView = imageView
not sure why I had to do that..
The reason is because you forgot set the position for the imageView.
You can change your code like this:
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)
imageView.image = logo
self.navigationItem.titleView = imageView
You have not set the frame of the image view. I have set frame of the image view and it works fine.
if let logo = UIImage(named: "download"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.navigationItem.titleView = imageView
}else{
print("Image not found...")
}

UIButton and UIScrollView

I have a UIScrollView that contains a long image, whose height is way more than a screen could hold at the current time. Now I want to place buttons all over the image. However, some of the buttons are below the view that you see when you haven't scrolled yet and after scrolling to the bottom of the image, the button I currently press is the one at the top that occupies the position of the screen itself when one hasn't scrolled yet. In short, not the button at the bottom is clicked when I scroll down and click on it, but the one at the top. How do I solve this? Note: my code is in Swift, so please write down code snippets in Swift and not Objective-C.
func makeImage(){
//might change the following to make it more dynamic later on
imageView = UIImageView(image: UIImage(named:brand.name + "-" + genderChoice.name + "-"+generalOverviewChoice.name+"-Detail.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
imageView.frame = CGRectMake(0, 0, 375, imageView.frame.size.height/2)
scrollView.contentSize = CGSizeMake(375,imageView.frame.size.height)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
}
func createButton(button: UIButton,x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat){
button.frame = CGRectMake(x, y, width, height)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)
}