Round Border With Background Image Button swift - ios

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.

Related

iOS - Set ImageView background color and mask

How can I set the background color of an ImageView for an icon and have it mask to bounds? So that the inner transparent parts of the image are then the background color?
Example: in this twitter icon, how can I set the inner white (transparent) parts of the icon to be a different color?
EDIT: What if the image is NOT a circle? But any shape.
You have to follow the following steps
imageView.image = UIImage(named: "yourImageName")?.withRenderingMode(.alwaysOriginal)
imageView.layer.cornerRadius = 14 //number of your choice
imageView.layer.masksToBounds = true
imageView.tintColor = .black
imageView.backgroundColor = .blue //Sets the background color
imageView.layer.cornerRadius = imageView.width/2 //Crops the image to be a circle
imageView.layer.masksToBounds = true //Sets the mask to bounds
If you want a custom shape you can do this:
create two imageViews (above each other)
first imageView use as usual
second imageView use as background shape
Code for the one in foreground
firstImageView.backgroundColor = .clear
Second imageView (in the background)
secondImageView.image = UIImage(named: "yourImage").withRenderingMode(.alwaysTemplate)
secondImageView.tintColor = yourBackgroundColor
secondImageView.backgroundColor = .clear

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` ...

How to show label outside the collectionView outline

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

How to center an image inside an UIButton without stretching in both direction in Swift?

I am making a custom keyboard and I need to centre the image of the shift icon inside the shift button. The image is 100px x 100px.
However, the button frame in portrait is 36pt x 38pt, and in landscape, it is 68pt x 32pt. I have tried using button image inset property to centre the image. However, it does not does so.
I tried myself and set the image edge insets to (25,25,25,25), the image centres in both sizes. However, the icon becomes too small to see.
shiftButton.setImage(UIImage(named: "shift"), forState: .Normal)
shiftButton.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0)
I am making the button programatically. And, I need the button to scale maintaining its aspect ratio i.e. 1:1. Its maximum width or height is always tied to the button's height itself. The image width/height should be maximum of button's height. And, it should not stretch in any way to distort the icon. However, the button itself can stretch.
I need the image to scale according to button frame but maintain its aspect ratio. And, it can down scale or up scale, but should always maintain the 1:1 aspect ratio.
Any suggestions for solving this problem?
Finally a solution came through. You must set the content mode for the image inside the UIButton.
The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton.
shiftButton.contentMode = .Center
shiftButton.imageView?.contentMode = .ScaleAspectFit
If you're using storyboard, double-check that you don't have anything set for the button title.
Add User Defined Runtime Attributes to your button.
In the screen shot below value 1 is equal to scaleAspectFit
imageView.ContentMode = scaleAspectFit
I was able to center the image by setting the content insets to 0 using the size inspector on the storyboard:
Set the button's content mode to Center:
shiftButton.contentMode = .Center
To work on UIButton with image is sort of tricky things. I think below two lines could solve your problem.
And there is a good post Aligning text and image on UIButton
// Keep button's image to its original ratio.
button.imageView?.contentMode = .scaleAspectFit
// Give button image a little space from all the four edges.
button.imageEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
A button in my project
lazy var downloadButton: TransitionButton = {
let button = TransitionButton()
button.translatesAutoresizingMaskIntoConstraints = false
let config = UIImage.SymbolConfiguration(pointSize: 26, weight: .bold, scale: .default)
let image = UIImage(systemName: "arrow.down", withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
button.backgroundColor = .systemPink
button.layer.cornerRadius = 12
button.addTarget(self, action: #selector(downloadButtonTapped(_:)), for: .touchUpInside)
button.spinnerColor = .white
return button
}()

Changing the Navigation Bar in Swift

I would simply like to know how to make my navigation bar look like the one on the left in the image below:
My app uses a navigation controller, and the navigation bar by default looks like the screen on the right in the image. I don't like it being so small and would like it higher and also to be able to change the colour and the font of the text used for the header.
I would appreciate any help in how to do this and where to place the code.
thanks
There are many ways to costumize your navigation bar:
//You can change the bar style
navigationController?.navigationBar.barStyle = UIBarStyle.Black
// You can change the background color
navigationController?.navigationBar.barTintColor = UIColor(red: 4 / 255, green: 47 / 255, blue: 66 / 255, alpha: 1)
// You can add a logo on it
let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
navBarImageView.contentMode = .ScaleAspectFit
let navBarImage = UIImage(named: "myNavBarLogo.png")
navBarImageView.image = navBarImage
navigationItem.titleView = navBarImageView
//You can change the text color
navigationController?.navigationBar.tintColor = UIColor.yellowColor()
//You can change the font
if let font = UIFont(name: "AppleSDGothicNeo-Thin", size: 34) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
You just need to play with those attributs to get the Navigation Bar as you want.
I hope that helps you!
You can change the navigation bar in Interface Builder.
You can change the background color by changing the bar tint. You can also change the title font, color, and etc., as you can see below:

Resources