UICollectionView not displayed to view
I have been stuck on this problem for nearly a week now. I have even reverted my code and wrote the same code as some tutorials but I cannot seem to get my UICollectionView
to be displayed to the view for the life of me. I have a lot of other code going on in my view controllers so I am going to display the code that pertains to the UICollectionView
. This is what I have so far:
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(100)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
view.addConstraints(horizontalConstraint)
view.addConstraints(verticalConstraint)
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.red
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
//took out constraints here just to make the warning mentioned below easier to follow.
}
I do have my protocols implemented but I didn't add them just to save the amount of code I'm posting. I do get this warning after the UARTModuleViewController
loads :
[LayoutConstraints] Unable to simultaneously satisfy constraints.
I've tried reasoning through it and can't seem to come to a solution. I do have a cell class as well but thought it was not necessary to include since I can't get the UICollectionView
to be displayed. The only other thing that I may add is that I have a UIImageView
in the middle of the view that I added in story board and put constraints on like so:
Does anyone have any suggestions as to what I may be doing wrong? Thanks in advance for any help or advice that can be given.
ios swift uicollectionview constraints nslayoutconstraint
|
show 1 more comment
I have been stuck on this problem for nearly a week now. I have even reverted my code and wrote the same code as some tutorials but I cannot seem to get my UICollectionView
to be displayed to the view for the life of me. I have a lot of other code going on in my view controllers so I am going to display the code that pertains to the UICollectionView
. This is what I have so far:
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(100)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
view.addConstraints(horizontalConstraint)
view.addConstraints(verticalConstraint)
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.red
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
//took out constraints here just to make the warning mentioned below easier to follow.
}
I do have my protocols implemented but I didn't add them just to save the amount of code I'm posting. I do get this warning after the UARTModuleViewController
loads :
[LayoutConstraints] Unable to simultaneously satisfy constraints.
I've tried reasoning through it and can't seem to come to a solution. I do have a cell class as well but thought it was not necessary to include since I can't get the UICollectionView
to be displayed. The only other thing that I may add is that I have a UIImageView
in the middle of the view that I added in story board and put constraints on like so:
Does anyone have any suggestions as to what I may be doing wrong? Thanks in advance for any help or advice that can be given.
ios swift uicollectionview constraints nslayoutconstraint
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under theUIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of theUIImageView
.
– demogorgon
Nov 23 '18 at 22:23
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30
|
show 1 more comment
I have been stuck on this problem for nearly a week now. I have even reverted my code and wrote the same code as some tutorials but I cannot seem to get my UICollectionView
to be displayed to the view for the life of me. I have a lot of other code going on in my view controllers so I am going to display the code that pertains to the UICollectionView
. This is what I have so far:
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(100)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
view.addConstraints(horizontalConstraint)
view.addConstraints(verticalConstraint)
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.red
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
//took out constraints here just to make the warning mentioned below easier to follow.
}
I do have my protocols implemented but I didn't add them just to save the amount of code I'm posting. I do get this warning after the UARTModuleViewController
loads :
[LayoutConstraints] Unable to simultaneously satisfy constraints.
I've tried reasoning through it and can't seem to come to a solution. I do have a cell class as well but thought it was not necessary to include since I can't get the UICollectionView
to be displayed. The only other thing that I may add is that I have a UIImageView
in the middle of the view that I added in story board and put constraints on like so:
Does anyone have any suggestions as to what I may be doing wrong? Thanks in advance for any help or advice that can be given.
ios swift uicollectionview constraints nslayoutconstraint
I have been stuck on this problem for nearly a week now. I have even reverted my code and wrote the same code as some tutorials but I cannot seem to get my UICollectionView
to be displayed to the view for the life of me. I have a lot of other code going on in my view controllers so I am going to display the code that pertains to the UICollectionView
. This is what I have so far:
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(100)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
view.addConstraints(horizontalConstraint)
view.addConstraints(verticalConstraint)
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.red
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
//took out constraints here just to make the warning mentioned below easier to follow.
}
I do have my protocols implemented but I didn't add them just to save the amount of code I'm posting. I do get this warning after the UARTModuleViewController
loads :
[LayoutConstraints] Unable to simultaneously satisfy constraints.
I've tried reasoning through it and can't seem to come to a solution. I do have a cell class as well but thought it was not necessary to include since I can't get the UICollectionView
to be displayed. The only other thing that I may add is that I have a UIImageView
in the middle of the view that I added in story board and put constraints on like so:
Does anyone have any suggestions as to what I may be doing wrong? Thanks in advance for any help or advice that can be given.
ios swift uicollectionview constraints nslayoutconstraint
ios swift uicollectionview constraints nslayoutconstraint
asked Nov 23 '18 at 21:28
demogorgondemogorgon
426315
426315
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under theUIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of theUIImageView
.
– demogorgon
Nov 23 '18 at 22:23
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30
|
show 1 more comment
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under theUIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of theUIImageView
.
– demogorgon
Nov 23 '18 at 22:23
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under the
UIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of the UIImageView
.– demogorgon
Nov 23 '18 at 22:23
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under the
UIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of the UIImageView
.– demogorgon
Nov 23 '18 at 22:23
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30
|
show 1 more comment
1 Answer
1
active
oldest
votes
According to comments:
- First of all, every time you use constraint in code, you must set
translatesAutoresizingMaskIntoConstraints = false
to the view you want to add constraint. - You're not telling the collection view to fill entire
MenuBar
space. - Constraints applied to
MenuBar
insetupMenuBar
are not enaugh to determine the size and position of the view.
These are changes you should apply to tell the MenuBar
to fill width, be 60 pt height and to position on bottom of the screen:
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
To tell the collection view to fill entire MenuBar
view do this in MenuBar
init(frame:)
method:
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
//took out constraints here just to make the warning mentioned below easier to follow.
}
As you can see in my code, I usually use "anchor" methods to apply constraint because are easier than visual format.
Wow thanks so much! Earlier I settranslatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!
– demogorgon
Nov 23 '18 at 22:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453124%2fuicollectionview-not-displayed-to-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to comments:
- First of all, every time you use constraint in code, you must set
translatesAutoresizingMaskIntoConstraints = false
to the view you want to add constraint. - You're not telling the collection view to fill entire
MenuBar
space. - Constraints applied to
MenuBar
insetupMenuBar
are not enaugh to determine the size and position of the view.
These are changes you should apply to tell the MenuBar
to fill width, be 60 pt height and to position on bottom of the screen:
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
To tell the collection view to fill entire MenuBar
view do this in MenuBar
init(frame:)
method:
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
//took out constraints here just to make the warning mentioned below easier to follow.
}
As you can see in my code, I usually use "anchor" methods to apply constraint because are easier than visual format.
Wow thanks so much! Earlier I settranslatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!
– demogorgon
Nov 23 '18 at 22:52
add a comment |
According to comments:
- First of all, every time you use constraint in code, you must set
translatesAutoresizingMaskIntoConstraints = false
to the view you want to add constraint. - You're not telling the collection view to fill entire
MenuBar
space. - Constraints applied to
MenuBar
insetupMenuBar
are not enaugh to determine the size and position of the view.
These are changes you should apply to tell the MenuBar
to fill width, be 60 pt height and to position on bottom of the screen:
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
To tell the collection view to fill entire MenuBar
view do this in MenuBar
init(frame:)
method:
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
//took out constraints here just to make the warning mentioned below easier to follow.
}
As you can see in my code, I usually use "anchor" methods to apply constraint because are easier than visual format.
Wow thanks so much! Earlier I settranslatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!
– demogorgon
Nov 23 '18 at 22:52
add a comment |
According to comments:
- First of all, every time you use constraint in code, you must set
translatesAutoresizingMaskIntoConstraints = false
to the view you want to add constraint. - You're not telling the collection view to fill entire
MenuBar
space. - Constraints applied to
MenuBar
insetupMenuBar
are not enaugh to determine the size and position of the view.
These are changes you should apply to tell the MenuBar
to fill width, be 60 pt height and to position on bottom of the screen:
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
To tell the collection view to fill entire MenuBar
view do this in MenuBar
init(frame:)
method:
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
//took out constraints here just to make the warning mentioned below easier to follow.
}
As you can see in my code, I usually use "anchor" methods to apply constraint because are easier than visual format.
According to comments:
- First of all, every time you use constraint in code, you must set
translatesAutoresizingMaskIntoConstraints = false
to the view you want to add constraint. - You're not telling the collection view to fill entire
MenuBar
space. - Constraints applied to
MenuBar
insetupMenuBar
are not enaugh to determine the size and position of the view.
These are changes you should apply to tell the MenuBar
to fill width, be 60 pt height and to position on bottom of the screen:
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
To tell the collection view to fill entire MenuBar
view do this in MenuBar
init(frame:)
method:
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
//took out constraints here just to make the warning mentioned below easier to follow.
}
As you can see in my code, I usually use "anchor" methods to apply constraint because are easier than visual format.
answered Nov 23 '18 at 22:43
Rico CrescenzioRico Crescenzio
661413
661413
Wow thanks so much! Earlier I settranslatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!
– demogorgon
Nov 23 '18 at 22:52
add a comment |
Wow thanks so much! Earlier I settranslatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!
– demogorgon
Nov 23 '18 at 22:52
Wow thanks so much! Earlier I set
translatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!– demogorgon
Nov 23 '18 at 22:52
Wow thanks so much! Earlier I set
translatesAutoresizingMaskIntoConstraints = false
but I didn't realize that the constraints weren't enough to determine size/position of the view. This worked perfectly. Thanks for the explanation as well!– demogorgon
Nov 23 '18 at 22:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453124%2fuicollectionview-not-displayed-to-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How should the Menu bar be displayed?
– Rico Crescenzio
Nov 23 '18 at 22:21
@RicoCrescenzio right now I'm just trying to get it to display. eventually it will sit under the
UIImageView
at the bottom of the screen. It will take up width of the screen and then the height will be almost to the bottom of theUIImageView
.– demogorgon
Nov 23 '18 at 22:23
You want the collection view to have same size of the parent view?
– Rico Crescenzio
Nov 23 '18 at 22:26
@RicoCrescenzio only the width will be the same width as the parent view. the height will be smaller. I don't have set values yet as I'm just trying to display it. It will probably be ~1/5 of the parent view height I'm guessing.
– demogorgon
Nov 23 '18 at 22:29
I mean, the collection view will be cover entire MenuBar?
– Rico Crescenzio
Nov 23 '18 at 22:30