0

In UIViewController class i am using this code :

 func handleMore() {
    let blackView = UIView()
    blackView.backgroundColor = UIColor.black
    view.addSubview(blackView)
    blackView.frame = view.frame

}

why it leaves white strip? https://drive.google.com/open?id=0Bz964D-WrU9KYktYdmhyR3dNY1U i really confused(

Ninja
  • 309
  • 7
  • 26
  • Set it to the bounds, not the frame. blackView.frame = view.bounds – Tim Oct 09 '17 at 13:17
  • i got that if i put instead of blackView.frame = view.frame -----> . blackView.frame = (collectionView?.frame)! view fills with dark but then what is the view property? and why is so small? and it doest not cover all windows without status bar and navig bar....? – Ninja Oct 09 '17 at 13:19
  • Print out the view's frame and you'll see its origin. What are you trying to achieve? – Tim Oct 09 '17 at 13:21
  • The white portion could be part of the navigation bar/ red view – Ameya Vichare Oct 09 '17 at 13:21

1 Answers1

0

Set the frame to bounds of the view, not the frame.

blackView.frame = view.bounds

I'm assuming you only care about the view's size, not its origin. So you should use bounds.

UIView frame, bounds and center

Tim
  • 8,932
  • 4
  • 43
  • 64
  • it logs 0.0, 64.0, 375.0, 603.0 but i han only menu bar below navigationbar and i added only this vert constraint view.addConstraintsWithFormat(format: "V:|[v0(50)]", views: [menuBar]) so do you have any idea what are these 14 points? – Ninja Oct 09 '17 at 13:43
  • 64 is the 44 point navigation bar, plus the 20 point status bar. By setting blackView.frame to view.frame you're giving blackView a y offset of 64 points. Hence why it is lower down the screen in your screenshot. If you set it to the bounds, this won't happen. – Tim Oct 09 '17 at 13:45
  • then why it is counting offset starting from the end of navigation bar? because if i print collectionView?frame it logs 0.0 0.0 375.0 603.0 whereas setting blackView.frame to collectionView?frame do not cover navigation bar? – Ninja Oct 09 '17 at 13:48
  • What are you trying to achieve? Can you draw a diagram? – Tim Oct 09 '17 at 13:50
  • 1. it strarting count origin not from window top edge because if i print collectionView?frame it logs 0.0 0.0 375.0 603.0 whereas setting blackView.frame to collectionView?frame do not cover navigation bar 2. why it is make offset of 64 points from the navigationbar bottom edge? – Ninja Oct 09 '17 at 13:54
  • i am really trying to get true intentions of UICollectionViewController's root view's layout – Ninja Oct 09 '17 at 13:56
  • i added a picture – Ninja Oct 09 '17 at 14:02
  • You haven't explained what you are trying to achieve so it's very difficult to help. But I suggest you read up on the difference between frame and bounds. And think about your view hierarchy. You can use the View Debugger in Xcode to help you understand this. If you are trying to add a view above the navigation bar, then you cannot add a subview to anything in view controller's view hierarchy as it exists completely beneath the navigation bar. – Tim Oct 09 '17 at 14:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156276/discussion-between-n-khasanov-and-tim). – Ninja Oct 09 '17 at 14:13