英文:
Swift Navigation Title Bar
问题
我是新手在Swift开发中。我已删除主要的故事板,因为我想尝试以编程方式开发一切。我想要添加带标题的导航栏。有什么建议:
class ViewController: UIViewController {
private let imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .white
return imageView
}()
private let button: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitle("随机照片", for: .normal)
button.setTitleColor(.black, for: .normal)
return button
}()
let colors: [UIColor] = [
.systemRed,
.systemBlue,
.systemCyan,
.systemGray,
.systemIndigo
]
override func viewDidLoad() {
super.viewDidLoad()
// 在加载视图后执行任何额外的设置。
view.backgroundColor = .systemRed
view.addSubview(imageView)
imageView.frame = CGRect(
x: 0,
y: 0,
width: 300,
height: 300
)
imageView.center = view.center
view.addSubview(button)
getRandomPhoto()
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
}
@objc func didTapButton() {
getRandomPhoto()
view.backgroundColor = colors.randomElement()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.frame = CGRect(
x: 30,
y: view.frame.size.height - 150 - view.safeAreaInsets.bottom,
width: view.frame.size.width - 60,
height: 55
)
}
func getRandomPhoto() {
let urlString = "https://source.unsplash.com/random/600x600"
let url = URL(string: urlString)!
guard let data = try? Data(contentsOf: url) else {
return
}
imageView.image = UIImage(data: data)
}
}
- 我已经尝试过这个,但没有出现标题。有什么帮助让导航标题栏出现。
英文:
I'm new in swift development. I have deleted the main storyboard because I wanted to try develop everything programmatically. I wanted to add the navigation bar with title. Any suggestion:
class ViewController: UIViewController {
private let imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .white
return imageView
}()
private let button: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitle("Random Photo", for: .normal)
button.setTitleColor(.black, for: .normal)
return button
}()
let colors: [UIColor] = [
.systemRed,
.systemBlue,
.systemCyan,
.systemGray,
.systemIndigo
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .systemRed
view.addSubview(imageView)
imageView.frame = CGRect(
x: 0,
y:0,
width: 300,
height: 300
)
imageView.center = view.center
view.addSubview(button)
getRandomPhoto()
button.addTarget(self, action: #selector(didTapButton), for: UIControl.Event.touchUpInside)
}
@objc func didTapButton(){
getRandomPhoto()
view.backgroundColor = colors.randomElement()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.frame = CGRect(
x:30,
y:view.frame.size.height-150-view.safeAreaInsets.bottom,
width: view.frame.size.width-60,
height: 55
)
}
func getRandomPhoto(){
let urlString = "https://source.unsplash.com/random/600x600"
let url = URL(string: urlString)!
guard let data = try? Data(contentsOf: url) else {
return
}
imageView.image = UIImage(data: data)
}
}
- I already tried this but it does not appear any title. Any help to appear the navigation title bar.
答案1
得分: 0
为了以编程方式设置导航标题栏:
override func viewDidLoad() {
super.viewDidLoad()
setNavigationTitleBar()
}
func setNavigationTitleBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 50, width: screenSize.width, height: 80))
let navItem = UINavigationItem(title: "导航标题栏")
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
请优先考虑上面的代码片段以编程方式设置导航标题栏。
英文:
To set the navigation title bar programmatically:
override func viewDidLoad() {
super.viewDidLoad()
setNavigationTitleBar()
}
func setNavigationTitleBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 50, width: screenSize.width, height: 80))
let navItem = UINavigationItem(title: "Navigation Title Bar")
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
Kindly prefer the above code snippet for set the navigation title bar programmatically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论