How to show label outside the collectionView outline - ios

In my CollectionViewCell I am putting a cell, after that I am putting a label on cell.
Now I want to show my label outside the collectionView outline.
Like This
I am set label inside the collection cell Like This
let cell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? CollectionViewCell
cell?.lblDate.text = dateArray[indexPath.row]
if indexPath.item == 9{
cell?.backgroundColor = UIColor(red: 245.0/255.0, green: 112.0/255.0, blue: 108/255.0, alpha: 1.0)
cell?.lblDate.font = UIFont(name: "Helvetica-Bold", size: 30.0)
cell?.clipsToBounds = false
}
But I get Like This

a) Check whether UILabel's superView is set not to clip:
or
superView.clipsToBounds = false
b) Place a view into to the bottom view of each cell, set background color to gray-green and clip its size by another subView. Red cell will not be clipped - the effect remains the same

take your red cell and make transform(CGAffineTransformScale) to make it bigger. Then unselect property clipToBounds of your UICollectionView

Related

UIStackView height, content inside in the middle not at top

I have a UIStackview, and when its filled with a bunch of things, its fine, However, when its filled with 1, or 2 (for example), it spreads them out and not keeps them at top. What did I do wrong?
This is what happens. I want them to be at the top and with No right under it.
for i in 0..<self.itemSpecificsArray.count {
let itemSpecificName = UILabel()
let itemSpecificValue = UILabel()
itemSpecificName.textColor = UIColor(red: 236.0 / 255.0, green: 91.0 / 255.0, blue: 110.0 / 255.0, alpha: 1.0)
itemSpecificName.textAlignment = .left
itemSpecificName.font = UIFont.boldSystemFont(ofSize: 18.0)
itemSpecificValue.textColor = UIColor.black
itemSpecificValue.textAlignment = .left
itemSpecificValue.font = UIFont.systemFont(ofSize: 15.0)
print(self.itemSpecificsArray[i].itemSpecificName)
print("** \(self.itemSpecificsArray[i].itemSpecificValue)")
itemSpecificName.text = self.itemSpecificsArray[i].itemSpecificName
itemSpecificValue.text = self.itemSpecificsArray[i].itemSpecificValue
self.mainSectionItemSpecifics.addArrangedSubview(itemSpecificName)
self.mainSectionItemSpecifics.addArrangedSubview(itemSpecificValue)
if (i == self.itemSpecificsArray.count) {
let view = UIView()
self.mainSectionItemSpecifics.addArrangedSubview(view)
}
}
For each index, it has a itemSpecificName and itemSpecificValue. If I need to put these two Labels inside of a View, then I can do that...That will probably fix it, wouldnt it?
UIStackView wants to stretch its arranged subviews to fill its own (the stack view's) bounds. If you don't want the labels stretched, you either need to change the constraints on the UIStackView so that it can shrink to fit its children, or add an arranged subview to the stack view that can absorb the extract space. For example, you can add a plain UIView with no constraints after the two labels. Auto layout will give the extra space of the stack view's bounds to the UIView.

Round Border With Background Image Button swift

I'm using this code to create round Border Button With Background Image
{
Button.layer.cornerRadius = clear.frame.width/2
Button.clipsToBounds = true
Button.layer.borderWidth = 5.0
Button.layer.borderColor = UIColor(displayP3Red: 91/256, green: 87/256, blue: 115/256, alpha: 1.0).cgColor
}
And this is the outcome:
I want create a margin between the Button and the rounded border to this outcome:
Thanks in advance
Instead of setting background image you can set it as image to UIButton.
let image = UIImage(named: "imageName")
yourButton.setImage(image, for: .normal)
Using Storyboard, from attribute inspector.
You can also set content, title and image insets (top, left, bottom and right) from size inspector.
Set Content Insets to set image and title both at a time.
Set Title Insets to set title at a time.
Set Image Insets to set image at a time.

UITableViewController AutoLayoutConstraints

I have drawn a border for the UITableViewCell
I have to set the leading and trailing constraints for the UITableViewCell, so that border will look correct.
To draw the border i have used the below code at cellForRowAt(:)
let today = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! DisplayTODOTaskCell
today.layer.masksToBounds = true
today.layer.cornerRadius = 0
today.layer.borderWidth = 1
today.layer.shadowOffset = CGSize(width: -1, height: 1)
let borderColor: UIColor = .black
today.layer.borderColor = borderColor.cgColor
When I used the below code , I have got the runtime error.
today.constraints(NSLayoutConstraint(item: today, attribute: .alignAllLeading, relatedBy: nil, toItem: self, attribute: self, multiplier: 1, constant: 10))
enter image description here
When I view from the phone left and right border are not visible.
I have used TableViewController.
I could draw the border for the cell by leaving space .
Need I achieve for subsequent cell in different section ,for that I need to know the height of the previous section.Please let me know how to achieve this
I have added below code in the UITableViewCell class.
override func layoutSubviews() {
self.frame = CGRect(x: 10.0, y: 5.0, width: 390.0, height: self.frame.size.height)
print("1111 Height = (self.frame.size.height)")
super.layoutSubviews()
}
Instead of customising Cell directly, Create UIView inside UITableViewCell.
Add UIView inside DisplayTODOTaskCellby using Storyboard. Give constraints, {top 2, left 5, bottom 2, right 5}.
Inside this UIView, you can design whatever you need.
Give, tableview and cell backgroundcolor as white.
let today = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! DisplayTODOTaskCell
today.createdView.layer.masksToBounds = true
today.createdView.layer.cornerRadius = 0
today.createdView.layer.borderWidth = 1
today.createdView.layer.shadowOffset = CGSize(width: -1, height: 1)
let borderColor: UIColor = .black
today.createdView.layer.borderColor = borderColor.cgColor

iOS: Sides of UITableViewCell showing a different color when cell background is changed (Xcode 8, Swift 3)

When implementing a UITableView, I added a background image and set the table view to transparent. However, the sides of the table remain clear even when I change the table view cells to a different color (in order to see the text better), as shown in this image: Table View Cell.
The code I added to get the inner part of the table view:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.accessoryView?.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.tintColor = UIColor(white: 1, alpha: 0.25)
}
And in viewWillAppear:
self.tableView.backgroundColor = UIColor(white: 1, alpha: 0.25)
I'm not sure why the sides don't show up as the same color. I thought it had something to do with the accessory button, but when attempting to change that as well, nothing was changed.
The contentView doesn't cover the whole cell. Try making the setting its background color to the clear color. Then only the cell's background view will show through.
Much of your code is sheer waste; it has nothing to do with the matter. The key lines are:
cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)
That's a silly thing to do, as you are overlaying the cell's translucent background color with another background color (the background color of the content view, which sits in front of the cell).
When you have a problem like this, solve it yourself by making a simpler, clearer test case. For example:
cell.backgroundColor = .red
cell.contentView.backgroundColor = .green
cell.accessoryType = .checkmark
That makes it perfectly obvious what's going on: the green content view partially obscures the red cell. Your code does exactly the same sort of thing.

Button in the footer of tableview section Swift3

I have a footer in the section of my tableView.
I try to add a button, in the center, and after to center my button.
I didn't find how I can center my button, I try to use view.center, or give him the width of all the parent view and after use a text align for center my button. (Personally I prefer use the second method .)
Actually my code is this one
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
let labelMore = UIButton()
footerView.addSubview(labelMore)
labelMore.sizeToFit()
labelMore.setTitle(footerText(status: "Test"), for: .normal)
labelMore.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
footerView.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
//labelMore.addTarget(self, action:#selector(footerMore(sender:)), for: .touchUpInside)
return footerView
}
And I got, the green square is actually my button, I erase from my code labelMore.frame = footerView.frame, because that don't work
edit: If my question is not clear I want center my button.
None of the code you showed "centers" the button, so it is not surprising that it is not centered. You do not set the frame.origin of labelMore at all, so its origin is zero — the top left corner, exactly as shown in the screen shot.
I erase from my code labelMore.frame = footerView.frame
Yes, well that was never going to work. It's just another case of confusing frame and bounds. Remember, the frame of the subview is in terms of the bounds of the superview. So you would change that code to
labelMore.frame = footerView.bounds
However, that isn't going to work either because footerView has no bounds — you haven't given it any size!
The real solution here is to position labelMore using autolayout. That way, no matter how footerView ends up being sized, labelMore will have the correct position within it. For example, let's assume that you want labelMore to be the same size as footerView:
let footerView = UIView()
let labelMore = UIButton()
footerView.addSubview(labelMore)
labelMore.topAnchor.constraint(equalTo: footerView.topAnchor).isActive = true
labelMore.bottomAnchor.constraint(equalTo: footerView.bottomAnchor).isActive = true
labelMore.leadingAnchor.constraint(equalTo: footerView.leadingAnchor).isActive = true
labelMore.trailingAnchor.constraint(equalTo: footerView.trailingAnchor).isActive = true
labelMore.translatesAutoresizingMaskIntoConstraints = false
// ... and remove the `sizeToFit` ...

Resources