我已经可以使用ff代码将背景图像添加到导航栏,但图像会被拉伸。有什么方法可以不被拉伸吗?
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "BG.jpg"), for: .default)
答案 0 :(得分:1)
你可以试试这个,
var mylist = [{"email":null,"school":"schoolA","courses":[{"name":"ABC","type":"chemistry"},{"name":"XYZ","type":"math"}]},{"email":null,"school":"schoolB"}]
const course = mylist.reduce((r, {courses}) => {
if (courses && !r) {
const course = courses.find(({type}) => type == 'chemistry');
if (course) r = course.name;
}
return r;
}, null)
console.log(course)
尝试代码中的其他选项。
<强>更新强>
您甚至可以尝试UINavigationBar.appearance().setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
UINavigationBar
以上代码可能会因您的最终要求而异。
答案 1 :(得分:0)
///此属性将图像设置为导航栏
public var navigationBarImage: UIImage? {
get {
return (navigationItem.titleView as? UIImageView)?.image
} set {
if newValue != nil {
let imageView = UIImageView(image: newValue)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
} else {
navigationItem.titleView = nil
}
}
}
答案 2 :(得分:0)
对于所有图像,您可以设置内容模式=
.scaleToFill (UIViewContentModeScaleToFill)
.scaleAspectFit (UIViewContentModeScaleAspectFit)
.scaleAspectFill (UIViewContentModeScaleAspectFill)
如果您不想看到超出界限的内容,您需要记住在包含的超级视图上将clipsToBounds设置为true
View.clipsToBounds = true