Swift 导航条中的一个item如何定位?
How can one position navigation bar item in Swift?
我有一个导航栏,目前只有一个后退按钮。我想在导航栏的右侧添加一个图像按钮,但我使用的图像比导航栏大,最终覆盖了后退按钮并且位置很奇怪。
这是代码:
let mapBtn = UIButton(type: .system)
mapBtn.setImage(#imageLiteral(resourceName: "map-1"), for: .normal)
mapBtn.frame = CGRect(x: 0,y: 0,width: 5,height: 5)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: mapBtn)
这是正在发生的事情的图像:
https://imgur.com/a/kzcwbGK
有没有办法给 mapBtn 添加一个约束,让它保持在右边?
尝试调整图片大小
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
if let newImage = UIGraphicsGetImageFromCurrentImageContext(){
UIGraphicsEndImageContext()
return newImage
}else{
return nil
}
}
let mapBtn = UIButton(type: .system)
let img = UIImage(named: "map-1")
let resizedImage = resizeImage(image: img, targetSize: CGSize(width: 5.0, height: 5.0)
mapBtn.frame = CGRect(x: 0,y: 0,width: 5,height: 5)
mapBtn.setImage(resizedImage, for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: mapBtn)
设置这个。
mapBtn.imageView?.contentMode = .scaleAspectFit
我有一个导航栏,目前只有一个后退按钮。我想在导航栏的右侧添加一个图像按钮,但我使用的图像比导航栏大,最终覆盖了后退按钮并且位置很奇怪。
这是代码:
let mapBtn = UIButton(type: .system)
mapBtn.setImage(#imageLiteral(resourceName: "map-1"), for: .normal)
mapBtn.frame = CGRect(x: 0,y: 0,width: 5,height: 5)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: mapBtn)
这是正在发生的事情的图像: https://imgur.com/a/kzcwbGK
有没有办法给 mapBtn 添加一个约束,让它保持在右边?
尝试调整图片大小
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
if let newImage = UIGraphicsGetImageFromCurrentImageContext(){
UIGraphicsEndImageContext()
return newImage
}else{
return nil
}
}
let mapBtn = UIButton(type: .system)
let img = UIImage(named: "map-1")
let resizedImage = resizeImage(image: img, targetSize: CGSize(width: 5.0, height: 5.0)
mapBtn.frame = CGRect(x: 0,y: 0,width: 5,height: 5)
mapBtn.setImage(resizedImage, for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: mapBtn)
设置这个。
mapBtn.imageView?.contentMode = .scaleAspectFit