เมนูด้านล่างแถบนำทางพร้อมภาพเคลื่อนไหวใน Swift

ปัญหาของฉันง่ายมาก ฉันไม่รู้วิธีแสดงรายการ (เมนู) ใต้แถบนำทาง เมื่อฉันคลิกรายการนั้น (บนแถบนำทาง)

ฉันต้องการทำสิ่งเดียวกันกับภาพนี้:

ป้อนคำอธิบายรูปภาพที่นี่

ฉันพยายามทำสิ่งนี้:

func doSomething(){

      let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0
      print(navigationBarHeight)
      let heightTotal = UIApplication.shared.statusBarFrame.height + navigationBarHeight

      DispatchQueue.main.async(execute: {
         appDelegate.infoView(message: "test", Yorigin: heightTotal, color: colorBlueFollow)
      })
   }

และสิ่งนี้ใน appDelegate:

func infoView(message: String, Yorigin: CGFloat ,color: UIColor){
      if infoViewIsShowing == false{
         infoViewIsShowing = true

//         let infoViewHeight = self.window!.bounds.height / 14.2
         let infoViewHeight = self.window!.bounds.height / 4.2
         let infoViewY = Yorigin - infoViewHeight

         let infoView = UIView(frame: CGRect(x: 0, y: infoViewY, width: self.window!.bounds.width, height: infoViewHeight))
         infoView.backgroundColor = color
         self.window!.addSubview(infoView)

         let infoLabelWidth = infoView.bounds.width
         let infoLabelHeight = infoView.bounds.height + UIApplication.shared.statusBarFrame.height/2

         let infoLabel = UILabel()
         infoLabel.frame.size.width = infoLabelWidth
         infoLabel.frame.size.height = infoLabelHeight
         infoLabel.numberOfLines = 0

         infoLabel.text = message
         infoLabel.font = UIFont(name: "HelveticaNeue", size: 11)
         infoLabel.textColor = UIColor.white
         infoLabel.textAlignment = .center

         infoView.addSubview(infoLabel)

         // Animate errorView
         UIView.animate(withDuration: 0.2, animations: {

            // Move down
            infoView.frame.origin.y = Yorigin

         }, completion: { (finished: Bool) in
            if finished{

               UIView.animate(withDuration: 0.2, delay: 3, options: .curveLinear, animations: {
                  // move up
                  infoView.frame.origin.y = infoViewY

               }, completion: { (finished: Bool) in
                  if finished {
                     infoView.removeFromSuperview()
                     infoLabel.removeFromSuperview()
                     self.infoViewIsShowing = false
                  }
               })

            }
         })


      }
   }

ปัญหาคือว่า มุมมองที่แสดงอยู่เหนือแถบนำทาง ไม่ใช่เอฟเฟกต์ที่ฉันต้องการ คุณมีความคิดเกี่ยวกับวิธีที่ฉันสามารถทำเช่นนั้นได้หรือไม่?


person KevinB    schedule 30.05.2017    source แหล่งที่มา


คำตอบ (3)


เส้นนี้

self.window!.addSubview(infoView)

วางไว้เหนือมุมมองอื่นๆ ทั้งหมดในหน้าต่าง (รวมถึงแถบนำทาง)

คุณควรค้นหาลำดับชั้นของตัวควบคุมมุมมอง โดยเริ่มจาก rootViewController เพื่อค้นหาลำดับชั้นที่จะเพิ่มมุมมอง

ดูคำตอบสำหรับคำถามนี้สำหรับตัวเลือกบางตัว: iPhone -- วิธีค้นหาจุดสูงสุด ดูตัวควบคุม

person Lou Franco    schedule 30.05.2017
comment
ขอบคุณสำหรับคำตอบ ฉันย้ายรหัสจาก AppDelegate ไปยัง View Controller ปัจจุบัน และฉันเปลี่ยน self.window! เพื่อตนเองดู มันง่ายขนาดนั้น ฮ่าฮ่า ! - person KevinB; 01.06.2017

คุณดู Cocoapods แล้วหรือยัง: https://cocoapods.org/?q=popover

มีส่วนประกอบพร้อมใช้งานมากมาย บางทีคุณควรหาส่วนประกอบที่เหมาะกับความต้องการของคุณ

คุณลองนำมาไว้ด้านหน้า infoView หรือไม่: นำมุมมองย่อยมาสู่มุมมองอื่นๆ ทั้งหมด

person Community    schedule 30.05.2017

สมมติว่าคุณเป็นคลาสย่อย UIViewController คุณสามารถตั้งค่าเป็นคุณสมบัติ 'navigationItem' ได้ดังต่อไปนี้

self.navigationItem.title = "Your text"

วิธีทั่วไปวิธีหนึ่งในการตั้งค่าข้อความหัวเรื่องบนตัวควบคุมการนำทางคือการแทนที่วิธีการมอบหมาย tableview ด้านล่างและคัดลอกข้อความ textLabel ของเซลล์ไปยังรายการ nav ของคลาสย่อย ViewController

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      self.navigationItem.title = tableView.cellForRow(at: indexPath)?.textLabel?.text
   }
person Ohmy    schedule 30.05.2017