UIButton ImageView contentmode doesn't work - ios

I just started an app for iOS in Swift, I'm doing the design programmatically (I mean I'm not using storyboard)
I have a UIButton with an image inside, I would like the image to fill the entire button. Right now it just show the image in my button, but smaller than the button, center in top of the button frame.
My code is :
let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize))
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.myExtension()
Here are the things I've tried (none of theses worked) :
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.autoresizingMask = .flexibleWidth
self.autoresizingMask = .flexibleHeight
self.imageView?.contentMode = .scaleAspectFill
If I just put the contentMode line it doesn't work either. The imageView doesn't return nil i've checked..
Thanks!

Set the contentMode of buttonLocation's imageView as scaleAspectFill or scaleToFill to cover the entire frame of button.
let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 500))
buttonLocation.setImage(UIImage(named: "image"), for: .normal)
buttonLocation.backgroundColor = .gray
buttonLocation.layer.cornerRadius = buttonLocation.frame.width / 2
buttonLocation.imageView?.contentMode = .scaleAspectFill //Here........
Example:

Try using below code and remove other properties which are being set for this button :
let buttonLocation = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
buttonLocation.setImage(UIImage(named: "cat"), for: .normal)
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = 50
buttonLocation.imageView?.contentMode = .scaleAspectFill
buttonLocation.clipsToBounds = true
self.view.addSubview(buttonLocation)
Here make sure that the image which you are setting in the button is larger than the frame of the button. You can change frame and corner radius values in the above code. I just tried with the above code and getting below output.
You can comment if this is not your expected output.
Hope this helps.

Answer on your question: https://stackoverflow.com/a/26263623/3047318
TLDR: set contentMode in viewDidLoad

you need to use setBackgroundImage property instead of setImage.
buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)

Maybe this can help you:
let buttonLocation = UIButton(type: .custom)
buttonLocation.frame = CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize)
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.imageView?.contentMode = .scaleAspectFill
buttonLocation.contentHorizontalAlignment = .fill
buttonLocation.contentVerticalAlignment = .fill
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.clipsToBounds = true
You need to add contentHorizontalAlignment and contentVerticalAlignment properties and don't forget set clipsToBounds to true to see the corner raius effect.

Related

Setting left view to the TextField with its bottom

I want to set the Leftview to the TextField. But I want to align it to the bottom of the Textfield. So that the leftview frame looks align to the TextField actual text. But I am not getting any luck.
here is how I am trying with it.
let btnSelectCountry = UIButton()
btnSelectCountry.setTitle(title, for: UIControlState.normal)
btnSelectCountry.titleEdgeInsets = UIEdgeInsetsMake(0, 0, -10, 0)
btnSelectCountry.backgroundColor = CommonUtils.hexStringToUIColor(hex: "#000000")
btnSelectCountry.frame = CGRect(x: 0, y: CGFloat(0), width: CGFloat(70), height: CGFloat(25))
btnSelectCountry.addTarget(self, action: #selector(self.selectCountryCallingCode), for: .touchUpInside)
etPhoneNum.leftView = btnSelectCountry
etPhoneNum.leftViewMode = .always
etPhoneNum.leftViewRect(forBounds: CGRect(x: 0, y: CGFloat(-100), width: CGFloat(70), height: CGFloat(25)))
But it is showing vertically aligned in the TextField. How can I move LeftView to bottom inside the TextField??
func setTextLeftView(){
txtField.leftView = UIImageView.init(image: image)
txtField.leftView?.frame = CGRect(x: 0, y: 5, width: 20 , height:20)
txtField.leftViewMode = .always
}
try this and if it does't work for you then let me know if would like to solve your problem :)

how to remove padding left UIBarButtonItem?

I would like to use a custom View as UIBarButtonItem left in an UINavigationController, is it possible to remove the left padding here?
Ill alrady tried:
let btn = UIButton()
btn.frame = CGRect(x: -50, y: -50, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: btn)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar , otherBtn ])
how to remove space left setting icon
update. tired this code:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height:
view.backgroundColor = .gray
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(DashboardTabBarController.openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
problem this user when clicked btn setting user
and other problem title navigation not center align
You should have noticed that whatever x value you provide to your custom view i.e, btn, it will always start from the same position because internally navigation bar doesn't count for position provided for that item and it always set the internally defined position as in below image, I set the background color to the custom button for better visuals.
So, the trick here is to put your custom button into another container view as below,
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
view.backgroundColor = .green
let btn = UIButton(frame: CGRect(x: -10, y: 10 , width: 30, height: 30))
btn.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
btn.backgroundColor = .yellow
btn.tintColor = .red
view.addSubview(btn)
let btn2 = UIButton(frame: CGRect(x: 20, y: 10, width: 30, height: 30))
btn2.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn2.contentMode = .scaleAspectFit
btn2.tintColor = .yellow
btn2.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn2)
let itemsContainer = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = [itemsContainer]
This will result into this,
Now, if you change the x, y, width, height of btn, it will adjust accordingly.
Please try this. It works fine for me. If this code not working properly then I will give you another solution.
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar])
Please add other button in View and set the width of view accordingy.It may hepls to you.Thank you
You can set imageInsets and set image this is example of left margin to set back image on position of default back button
let rightBtn = UIBarButtonItem(image: UIImage(named: "BackArrow"), style: .done, target: self, action: #selector(actnClose))
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
self.navigationItem.setLeftBarButton(rightBtn, animated: true)
Set the x value of cRect to zero:
btn.frame = CGRect(x: 0, y: -50, width: 44, height: 50)
The less painful and actually cleanest solution was to hide the left button and just add a subview directly to the navigationBar and customise it's position and size with the good old constraints. Worked like a charm. Haven't tested it in every situation but seems to work fine.

UINavigationBar set background image center swift..?

I'm placing the background image this:
self.navigationController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
but, repeat the image. No I want it to be repeated to me and Put it center.. ?
Here's how to fix it using UIAppearance and UIImage API:
// disable image stretching by defining left and top caps.
let navbarImage = image.stretchableImage(withLeftCapWidth: 1, topCapHeight: 1)
UINavigationBar.appearance().setBackgroundImage(navbarImage, for: .default)
Example before:
and after the fix:
Try with below method which worked for me.
var headerview = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
let imgview = UIImageView(frame: CGRect(x: 75, y: 0, width: 150, height: 44))
imgview.image = UIImage(named: "ImageName")
imgview.contentMode = UIViewContentMode.scaleAspectFit
headerview.addSubview(imgview)
self.navigationController?.navigationBar.topItem?.titleView = headerview

Bound the button to top right corner of the view iOS swift

I have a view in which i have multiple labels and a button i want the button to be bound to the top right corner
when i apply the add Missing constraints layout the button gets separate position for different devices Please help me to resolve this
NEVER use add missing constraints. This is a bad way of constructing your layout.
The easiest way to get your desired look is to decide on the size you want your button to be (eg. 30x30)
Then you add the following constraints to your button:
This sets the button's width & Height:
Then pin it to the right top corner:
//Just in case if anybody needs it with code:
//I have added 4 buttons to a custom view's each corner:
var customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
customView.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
customView.backgroundColor = UIColor.red //give color to the view
customView.center = self.view.center
let crossButton = UIButton(frame: CGRect(x: -10, y: -10, width: 30, height: 30))
crossButton.layer.cornerRadius = 15
crossButton.backgroundColor = UIColor.black
crossButton.setImage(UIImage(named: "cross.png"), for: .normal)
crossButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
crossButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let flipButton = UIButton(frame: CGRect(x: customView.frame.width-15, y: -10, width: 30, height: 30))
flipButton.layer.cornerRadius = 15
flipButton.backgroundColor = UIColor.black
flipButton.setImage(UIImage(named: "done.png"), for: .normal)
flipButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
flipButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let scaleButton = UIButton(frame: CGRect(x: -10, y: customView.frame.height-20, width: 30, height: 30))
scaleButton.layer.cornerRadius = 15
scaleButton.backgroundColor = UIColor.black
scaleButton.setImage(UIImage(named: "done.png"), for: .normal)
scaleButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
scaleButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let editButton = UIButton(frame: CGRect(x: customView.frame.width-20, y: customView.frame.height-20, width: 30, height: 30))
editButton.layer.cornerRadius = 15
editButton.backgroundColor = UIColor.black
editButton.setImage(UIImage(named: "done.png"), for: .normal)
editButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
editButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
customView.addSubview(crossButton)
customView.addSubview(flipButton)
customView.addSubview(scaleButton)
customView.addSubview(editButton)
self.view.addSubview(customView)
}
func crossButtonTapped(_ sender:UIButton) {
print("Cross Button was tapped")
}

How to remove specific border on an UIButton and an UIView?

I have an UIButton and an UIView where i want to remove bottom border for the button and top border for the view. The image below show an UIButton. When I press this UIButton the UIView will be added as subview (like a dropdown menu), But I want the button and the view merge with each other so it will look like on "box".
I know how to set border width and color:
self.layer.borderWidth = 1
self.layer.borderColor = UIColor(CGColor: "#616366".CGColor).CGColor
But I don't know if it is possible to remove on border line. Hope you guys can help - Thank you
The easiest way is going to be to change your layout a little bit so that rather than adding the UIView as a subview of the UIButton you add them both as siblings to a container view, and draw the border on the container view.
Kind of like this:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
button.setTitle("Button", forState: .Normal)
button.titleLabel?.textColor = UIColor.blackColor()
let container = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
container.addSubview(button)
container.frame = button.frame
container.backgroundColor = UIColor.whiteColor()
container.layer.borderWidth = 5
container.layer.borderColor = UIColor.blackColor().CGColor
let added = UIView(frame:CGRect(x: 0, y: 44, width: 100, height: 200))
added.backgroundColor = UIColor.blueColor()
container.addSubview(added)
container.frame.size.height += added.frame.size.height
XCPlaygroundPage.currentPage.liveView = container

Resources