2015-11-29 25 views
6

Vì vậy, tôi đang làm việc trên một ứng dụng đơn giản, nơi tôi sử dụng chế độ xem bảng. Chế độ xem bảng này hiển thị tên của "người chơi". Nhưng để tôi thêm người chơi vào chế độ xem bảng, tôi muốn cửa sổ bật lên được hiển thị với trường văn bản nơi bạn cung cấp tên.Làm cách nào để tạo chế độ xem bật lên tùy chỉnh nhanh chóng?

Bây giờ tôi đã đọc về việc tạo tệp xib hoặc nib, nhưng tôi không chắc chắn cách "tải" cửa sổ bật lên.

Cách tiếp cận tốt nhất cho điều này là gì?

Trông như thế này:

Pop up View

enter image description here

Trả lời

10

bạn muốn tạo ra một phong tục UIView với tất cả các đối tượng được tôn trọng cần thiết, từ viewDidLoad Kiểm Soát Tài Chính của bạn(), bạn sẽ giấu nó.

customView.hidden = true 

Bất cứ khi nào người dùng của bạn muốn thực hiện một số hành động hoặc công việc, bạn thôi ẩn nó và khi người dùng hoàn thành sau đó giấu nó một lần nữa hoặc loại bỏ từ SuperView.

customView.hidden = false 

Dưới đây có một số mã để giúp bạn bắt đầu

private var customView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     customView.hidden = true 
    } 

    private func loadCustomViewIntoController() { 
     let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200) 
     customView = UIView(frame: customViewFrame) 

     view.addSubview(customView) 

     customView.hidden = false 

     // any other objects should be tied to this view as superView 
     // for example adding this okayButton 

     let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50) 
     let okayButton = UIButton(frame: okayButtonFrame) 

     // here we are adding the button its superView 
     customView.addSubview(okayButton) 

     okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside) 

    } 


    func didPressButtonFromCustomView(sender:UIButton) { 
     // do whatever you want 
     // make view disappears again, or remove from its superview 
    } 


    @IBAction func rateButton(sender:UIBarButtonItem) { 
     // this barButton is located at the top of your tableview navigation bar 
     // when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard 

      loadCustomViewIntoController() 
    } 

Check it out github project này, Nó đóng cửa để được sản xuất sẵn sàng, nó mang lại cho bạn một cách tốt hơn để đối phó (hiện tại và bỏ qua) với UIViews

Nếu bạn chỉ muốn tên của người chơi, hãy sử dụng UIAlertController cont aining một textfield

+0

Tôi đã làm việc trên tùy chọn đó, vấn đề là trên bảng câu chuyện là loại khó thực hiện kể từ khi tôi làm việc với một tableview và không có nơi để đặt "UIView". Hoặc ít nhất tôi không biết cách làm điều đó. Nếu tôi thêm chế độ xem vào đầu, đó là chế độ xem cho tiêu đề hoặc ở dưới cùng là chế độ xem cho chân trang. Tôi sẽ thêm ảnh chụp màn hình vào bài đăng gốc của ý tôi. –

+0

bạn không cần phải kéo UIView trên đầu trang của tableview ... bạn có thể tạo UIView qua mã bạn muốn xem một ví dụ? – Lamar

+0

Xin vui lòng .. Tôi đã bao quanh đầu của tôi xung quanh này. –

8

Trong kịch bản

  • Trong kịch bản tạo ra một UIViewController và thiết kế nó theo yêu cầu của bạn.
  • Set lớp tùy chỉnh như anotherViewController và Storyboard_ID như another_view_sid
  • Tạo một tài khoản mới Cocoa Touch Class như anotherViewController và lớp con của UIViewController

Trong viewController

let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController 

    self.addChildViewController(popvc) 

    popvc.view.frame = self.view.frame 

    self.view.addSubview(popvc.view) 

    popvc.didMove(toParentViewController: self) 

Trong anotherViewController

override func viewDidLoad() { 
    super.viewDidLoad() 
     showAnimate() 
    } 
} 

@IBAction func Close_popupView(_ sender: Any) { 
    removeAnimate() 
} 

func showAnimate() 
{ 
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
    self.view.alpha = 0.0 
    UIView.animate(withDuration: 0.25, animations: { 
     self.view.alpha = 1.0 
     self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) 
    }) 
} 

func removeAnimate() 
{ 
    UIView.animate(withDuration: 0.25, animations: { 
     self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
     self.view.alpha = 0.0 
    }, completion: {(finished : Bool) in 
     if(finished) 
     { 
      self.willMove(toParentViewController: nil) 
      self.view.removeFromSuperview() 
      self.removeFromParentViewController() 
     } 
    }) 
} 
+0

rất hay, đang hoạt động! –

Các vấn đề liên quan