I'm getting the warning:
'imageEdgeInsets' was deprecated in iOS 15.0
When setting UIButton imageEdgeInsets like so:
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
I tried setting imageEdgeInsets like so:
UIImage(named: "filter")?.withRenderingMode(.alwaysTemplate).withAlignmentRectInsets(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
Both where top, left, right and bottom are positive or negative values, no luck. Anyone who knows the version of
imageEdgeInsets
That's not deprecated in iOS 15?
All help is appreciated,
Kind regards :)
iOS 15 Apple introduced 3 new options to control padding and insets.
.titlePadding : Padding between the title and subtitle labels.
.imagePadding : Padding between the button’s image and text.
.contentInsets: Padding from the button’s content area to its bounds.
Using the above option you can manage and set your button style according.
You can check this article for more. Source and Image
So your code should be like this
var configuration = UIButton.Configuration.filled()
configuration.title = "Title"
configuration.image = UIImage(systemName: "swift")
configuration.titlePadding = 10
configuration.imagePadding = 10
configuration.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
Swift now comes default with different types of buttons that you can setup up from iOS 15 onwards. Use UIButton.Configuration to set up your button. For your case, you can use,
config.imagePadding = 5
An example can be seen below:
var filled = UIButton.Configuration.filled()
filled.title = "Filled button"
filled.buttonSize = .large
filled.subtitle = "With subtitle even"
filled.image = UIImage(systemName: "bubble.left.fill")
filled.imagePlacement = .trailing
filled.imagePadding = 5
let button = UIButton(configuration: filled, primaryAction: nil)
Image and Code credits to nemecek.be
Related
I have a UIButton with the label on the left side and UIImageView on the right side. The button is used to open a UIPicker. When a value is picked in the picker the same value is shown in button title. When the title changes (or, more accurately, when to uilabel has a width that screws the UI up) the title and icon is moved and the UI does not look good.
When a title with too long text is used the word is clipped and when it's too short the alignment is messed up.
I've tried changing the label frame so it can be constant, whatever the text, and left aligning the text so the jumping stops. I added adjustsFontSizeToFitWidth = true which kind of works, but with a longer title the text will get too small. I've also tried recreating/rerendering the button when the title changes but all attempts fail.
lazy var sortButton = { () -> UIButton in
let btn = UIButton()
btn.addTarget(self, action: #selector(sortButtonPressed), for: .touchUpInside)
btn.setTitle(NSLocalizedString("Sortera", comment: ""), for: .normal)
btn.titleLabel?.text = btn.titleLabel?.text?.uppercased()
btn.setImage(UIImage(named: "ios-down"), for: .normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitleColor(Colors.FILTER_BUTTON_TEXT_COLOR, for: .normal)
btn.titleLabel?.adjustsFontSizeToFitWidth = true
btn.titleLabel?.font = UIFont(name: Fonts.AkzidenzGroteskProMd, size: 16)
btn.backgroundColor = Colors.BUTTON_BACKGROUND_GRAY
btn.imageView?.contentMode = .scaleAspectFit
btn.imageEdgeInsets = UIEdgeInsets(top: 16, left: (btn.titleLabel?.frame.size.width)! - buttonInsideOffset/2, bottom: 16, right: -(btn.titleLabel?.frame.size.width)! + buttonInsideOffset/2)
btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(btn.titleLabel?.frame.size.width)! + buttonInsideOffset, bottom: 0, right: (btn.titleLabel?.frame.size.width)! - buttonInsideOffset)
return btn
}()
I want all button to like like this, whatever the title text:
However, when the text is too small it looks like this:
or when it's too long:
You can do it in many way but the simplest way :
Take a UIView and then others two-element (a label a imageView) set in this View and make it look like button then set constraint as you want. Then addTarget to label and do all functionality to that that target selector method.
If you dont want button image to be shifted to the right or left then you have to constraint independent of the button's title label
btn.imageView?.frame = CGRect(x: 0, y: 0, width: 20, height: 20) // Or any size you want
// NB: I ommited left insets intentionally
btn.imageEdgeInsets.top = 16
btn.imageEdgeInsets.bottom = 16
btn.imageEdgeInsets.right = 16
Then constraint your label dependent to the imageView position, that way only label size will change without affecting the position of image.
btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(btn.titleLabel?.frame.size.width)! + buttonInsideOffset, bottom: 0, right: btn.imageView?.frame.width + 10)
Lastly, Since the frame is fixed size, I think you need to limit the font scaling factor to the min size you want and truncate the tail when that size is reached. If you don't want to truncate tail then you have to enable multiline titleLabel (Which I think you don't want this).
btn.titleLabel?.minimumScaleFactor = 0.5 // Or whatever minimum scale you wish
btn.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail // Since button size is fixed and you want to limit font size then the best option is to truncate tail
Hello All i am creating Tab bar programmatically, in my functionality there need to be selected tab filled with custom color, i have achieved this so far using below code and the following output see screenshot.
tabBarController.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: colors.AppSkyBlue, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: .stretch)
Clearly see the white space be around selected tab, ho do i remove this from all side. Any help would be appreciated well.
You Could simple add a subview on top of UITabBar and give the bg color same as the color of tab bar - so it blends with it
var lineView = UIView(frame: CGRect(x: 0, y: 0, width:tabBarController.tabBar.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.grey
tabBarController.tabBar.addSubview(lineView)
This is one of the options.
I have a uibutton, i want to set text dynamically at run time with padding top: 10, bottom 10 and left: 10 and right: 10, once i set text button title cliped. Any one can provide solution.
You must be attentive to fontSize of the button. Setting text programmatically doesn't automatically fits the text. Following code will help you.
//Swift
let btnLabel = yourButton.titleLabel
btnLabel.adjustsFontSizeToFitWidth = true
//Objective C
UILabel *btnLabel = yourButton.titleLabel;
btnLabel.adjustsFontSizeToFitWidth = true
Happy Coding <3
I implemented a UICollectionView with horizontal scrolling. My cells are simply thumbnails (UIImageView) with a size of 50x50.
let cvLayout = UICollectionViewFlowLayout()
cvLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
cvLayout.itemSize = CGSize(width: 50, height: 50)
cvLayout.scrollDirection = .horizontal
All works fine, as long as the UICollectionView's height is above ca. 100. If I set its height any lower, the images don't appear anymore and the view looks messed up. I tried playing around with all available settings, nothing changes this.
Does the UICollectionView have a minimum height requirement or is this something I could solve with a Custom Layout?
I appreciate any hint!
Grasping a straws a little unless you update the question for more detail. I am going to say it is one of two things. But most likely the first.
1) You build your layout like in the current question but you never assign it.
let cvLayout = UICollectionViewFlowLayout()
cvLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
cvLayout.itemSize = CGSize(width: 50, height: 50)
cvLayout.scrollDirection = .horizontal
//possibly not assigning it
collectionView.collectionViewLayout = cvLayout
2) you don't have constraints on your imageview but my guess is this is not the case.
Again, I am just taking a stab at it. If you comment below if neither of these fix the issue I will update the answer provided I have more information. I can set the height to 50 and it fits perfect.
I am trying to create a UIButton that contains a UIImage and text. So far I have managed to do that but I can't make it look pretty because the button width is half of the screen size (and this means the proportions are not right on big screens when I set the image)
Is there a way to position the Image exactly before the text Label in the button? Now I am doing this:
economicsButton.imageEdgeInsets = UIEdgeInsets(top: 15,left: 10,bottom: 15,right: 30)
economicsButton.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
Try this
let somespace: CGFloat = 10
self.economicsButton.setImage(UIImage(named: "cross"), forState: UIControlState.Normal)
self.economicsButton.imageEdgeInsets = UIEdgeInsetsMake(0, self.economicsButton.frame.size.width - somespace , 0, 0)
print(self.economicsButton.imageView?.frame)
self.economicsButton.titleEdgeInsets = UIEdgeInsetsMake(0,(self.economicsButton.imageView?.frame.width)! + somespace, 0, 10 )
check the original answer Align image on right side