How do I place the stepper value between the - and + signs? - swift

I am programatically trying to customize a UIStepper. I'd like to place the stepper.value between the - and + buttons and I want to change the background of the stepper to white.
How would I do that?
Here is what I have that does not work:
let stepper = UIStepper(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
stepper.minimumValue = 0
stepper.center = self.contentView.center
stepper.backgroundColor = .white
stepper.layer.backgroundColor = UIColor.white.cgColor
stepper.stepValue = 1
stepper.tintColor = UIColor(hex: Constants.Colors.primary)
stepper.setIncrementImage(UIImage(named: "icon_cart_plus"), for: .normal)
stepper.setDecrementImage(UIImage(named: "icon_cart_minus"), for: .normal)

Related

Swift UIButton Image size distorted in BarButtonItem

I want to add an UIButton to the navigationItem.leftBarButtonItem.
I want the UIButton to be 24x24px and scale the Image down.
But the Button got distorted... If I change the UIImage to a smaller image everything is fine. -> see pics
What can I do apart from the scaleAspectFit ??
let userProfilePic = UIButton()
userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
userProfilePic.contentMode = .scaleAspectFit
userProfilePic.clipsToBounds = true
userProfilePic.layer.borderWidth = 0.5
userProfilePic.layer.borderColor = UIColor.white.cgColor
userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2
userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)
Thank you!
Try this code
The is problem appears on ios 11+ , UIBarButtonItem use autolayout on ios 11+ other use frames
let userProfilePic = UIButton()
userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
userProfilePic.contentMode = .scaleToFill
userProfilePic.clipsToBounds = true
userProfilePic.layer.borderWidth = 0.5
userProfilePic.layer.borderColor = UIColor.white.cgColor
userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2
if #available(iOS 11, *) {
userProfilePic.widthAnchor.constraint(equalToConstant: 24.0).isActive = true
userProfilePic.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
}
userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)

Creating dynamic multiple UIButton programmatically

I am creating my UI programmatically, and this is how I create a button
let button: UIButton = {
let button = UIButton()
button.setTitle("Try1",.normal)
return button
}
And this is what I want to do. For example movie1 has got 2 categories, Dram & Horror I just want to show 2 of them.
Movie2 has got 4 categories, Dram&Horror&Sport& War i just want to show 4 of them.
And now Im stuck I have NO idea what to do. I know you guys gonna give minus rep but I need help.
Done! Solution is below
First I have made a struct
struct Category {
let title: String
let color: UIColor
init(title: String, color: UIColor) {
self.title = title
self.color = color
}
}
Then I made an empty array type of Category Struct
var categories = [Category]()
/* I appended some stuff manually to this array */
categories.append(Category(title: "Dram", color: Colors.cherry))
categories.append(Category(title: "Sport", color: Colors.lightGreen))
categories.append(Category(title: "Horror", color: Colors.purple))
categories.append(Category(title: "War", color: Colors.darkBlue))
/* Then I made a for loop */
for category in categories{
let button = UIButton()
button.createButton(title: category.title, titleColor: .white, fontType: "bold", fontSize: 17, background: category.color, cornerRadius: 4)
stackView.addArrangedSubview(button)
}
Use Horizontal StackView for that. In StackView you can add as much arranged subviews as you want and it will be automatically adjusting to the available space. Just setup constraints for StackView and rest will just work out of the box.
You can add your button to StackView using:
stackView.addArangedSubview(button)
To create button you can simple use for loop:
for i in 1...3 { //change 3 to your value
let button = UIButton()
stackView.addArangedSubview(button)
}
If you want to create multiple buttons in horizontal then add button in scrollview
Swift 4.0
func ButtonCreation() {
yourScrollviewname.isScrollEnabled = true
yourScrollviewname.isUserInteractionEnabled = true
let numberOfButtons = 16
let numberofRows = 1
var count = 0
var px = 0
var py = 0
for _ in 1...numberofRows {
px = 0
if count < numberOfButtons {
for j in 1...numberOfButtons{
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px, y: 0, width: 80, height: 27)
Button.layer.borderWidth = 1.0
Button.layer.masksToBounds = true
Button.setTitleColor(UIColor.black, for: .normal)
print(colorArr[j-1])
Button.setTitle(colorText[j-1], for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 50
}
}else{
for j in numberOfButtons/2...numberOfButtons {
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px+10, y: py+10, width: 80, height: 427)
Button.backgroundColor = UIColor.black
Button.setTitle("Hello \(j) ", for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 30
}
}
py = Int(yourScrollviewname.frame.height)-70
}
yourScrollviewname.contentSize = CGSize(width: px, height: py)
}
Swift 3.0 ...
let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356)) // set your postion
stackview.axis = . Horizontal
stackview.spacing = 10.0
stackview.alignment = .Fill
stackview.distribution = .EqualSpacing
for i in 1...3 {
let button = UIButton()
stackview.addArangedSubview(button)
}

Creating a button image with padding bottom in Swift

I'm creating a system of Filters.
I'm trying to do that:
Currently I'm using the setBackgroundImage func, and I don't have this little rect below the picture:
filterButton.setBackgroundImage(imageForButton, for: .normal)
I tried functions like this one:
filterButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
But it's not effective if I keep the setBackgroundImage func.
If I use setImage(), the results is good but the click is not effective any more.
filterButton.setImage(imageForButton, for: UIControlState.normal)
Do you have an idea to do what I would like ?
The full snippet:
for i in 0 ..< CIFilterNames.count {
itemCount = i
// Button properties
let filterButton = UIButton(type: .custom)
// filterButton.frame = CGRectMake(xCoord, yCoord, buttonWidth, buttonHeight)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = itemCount
filterButton.addTarget(self, action: #selector(self.filterButtonTapped(_:)), for: .touchUpInside)
// filterButton.layer.cornerRadius = 6
// filterButton.clipsToBounds = true
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: originalImage.image!)
let filter = CIFilter(name: "\(CIFilterNames[i])" )
filter!.setDefaults()
filter!.setValue(coreImage, forKey: kCIInputImageKey)
let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
let imageForButton = UIImage(cgImage: filteredImageRef!)
filterButton.backgroundColor = UIColor.red
filterButton.setImage(imageForButton, for: UIControlState.normal)
//filterButton.imageEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 15,right: 0)
//filterButton.setBackgroundImage(imageForButton, for: .normal)
filterButton.setTitle("A\(i)", for: .normal)
// filterButton.backgroundImage.contentMode = .scaleAspectFill
filterButton.setTitleColor(UIColor(red: 22 / 255, green: 21 / 255, blue: 22 / 255, alpha: 1), for: .normal)
filterButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
filterButton.titleEdgeInsets.top = 140
// Add Buttons in the Scroll View
xCoord += buttonWidth + gapBetweenButtons
filtersScrollView.addSubview(filterButton)
}
It looks like you are trying to do everything in a single UIButton. I would suggest creating a custom element to handle this.
There are a few ways you could setup a custom UI element with the functionality that you want:
Use a UILabel to display the "A1" label with the colored background and display the image itself in a separate UIImageView. These could be overlaid with a invisible UIButton to receive the touchUpInside event and pass to your controller.
Alternatively, you could create a custom UIControl add add a UILabel and UIImageView as subviews. Disabling userInteraction on the subviews should allow the UIControl to receive the touch events. This could be contained in a single reusable class as if it's one UI element.

How to set button width based on text for dynamically created button in swift 3?

I created the dynamic buttons. I need to change the width of button based on tittle label text. Here is my code.
for i in 0..<holdingSize {
let button = UIButton(type: .custom)
if i == 0 {
frame = CGRect(x: 10, y: 5, width: 100, height: 30)
}else{
buttonY = buttonY + 110
frame = CGRect(x: buttonY, y: 5, width: 100, height: 30)
}
button.setTitle("\(arrayOfHoldings[i])", for: UIControlState.normal) // We are going to use the item name as the Button Title here.
button.titleLabel?.text = "\(arrayOfHoldings[i])"
button.titleLabel?.font = UIFont(name: (button.titleLabel?.font.fontName)!, size: 15)
button.setTitleColor(Colors.green, for: .normal)
button.sizeToFit()
}
Try this
func labelSizeWithString(text: String,fontSize: CGFloat, maxWidth : CGFloat,numberOfLines: Int) -> CGRect{
let font = UIFont.systemFontOfSize(fontSize)//(name: "HelveticaNeue", size: fontSize)!
let label = UILabel(frame: CGRectMake(0, 0, maxWidth, CGFloat.max))
label.numberOfLines = numberOfLines
label.font = font
label.text = text
label.sizeToFit()
return label.frame
}
This will give you the frame of the label, you can set the height of your button from that.
FOR SWIFT 3.0
func labelSize(for text: String,fontSize: CGFloat, maxWidth : CGFloat,numberOfLines: Int) -> CGRect{
let font = UIFont.systemFont(ofSize: fontSize)//(name: "HelveticaNeue", size: fontSize)!
let label = UILabel(frame: CGRect(x: 0, y: 0, width: maxWidth, height: CGFloat.leastNonzeroMagnitude))
label.numberOfLines = numberOfLines
label.font = font
label.text = text
label.sizeToFit()
return label.frame
}

Force UIButton titleLabel text to show Swift

I have a button that is made in the following manner. The label of my button is truncated as I have shown at the bottom
let width = self.view.frame.size.width
let label = UIButton()
// label setup
label.setTitle("Going Out", for: .normal)
label.titleLabel?.font = UIFont(name: "avenir-next-regular", size: 8)
label.setTitleColor(.black, for: .normal)
label.layer.zPosition = 8
label.titleLabel?.textAlignment = .center
label.frame = CGRect(x: 3 * width / 32, y: width / 6, width: width / 6, height: width / 8)
view.addSubview(label)
This produes a button with the text: Go..Out
How can I force this to show: Going Out
While NOT sizeToFit() and shrinking my UIButton
Try below, should work:
label.titleLabel?.adjustsFontSizeToFitWidth = true