How does UIEdgeInsetsMake work?
According to the documentation:
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.
During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.
So you only need to use the amount of pixels you want to make unstretchable in the values of the UIEdgeInsetsMake
function.
Say you have an image of 21x50 points (21x50 pixels in standard definition, 42x100 pixels in Retina "@2x" definition) and want this image to be horizontally stretchable, keeping the 10 points on the left and on the right untouched when stretching the image, but only stretch the 1-point-wide band in the middle. Then you will use UIEdgeInsetsMake(0,10,0,10)
.
Don't bother that they are floats (that's useful for subpixelling resizing for example, but in practice you will probably only use integers (or floats with no decimal parts)
Be careful, this is an iOS5+ only method, not available prior iOS5. If you use pre-iOS5 SDK, use stretchableImageWithLeftCapWidth:topCapHeight:
instead.
[EDIT] Some tip I use since some time, as I never remember in which order the fields of the UIEdgeInsets
structure are — and in which order we are supposed to pass the arguments to UIEdgeInsetsMake
function — I prefer using the "designated inits" syntax like this:
UIEdgeInsets insets = { .left = 50, .right = 50, .top = 10, .bottom = 10 };
Or when an explicit cast is needed:
UIImage* rzImg = [image resizableImageWithCapInsets:(UIEdgeInsets){
.left = 50, .right = 50,
.top = 10, .bottom = 10
}];
I find it more readable, especially to be sure we don't mix the different borders/directions!
How to get the curent UIEdgeInsetsMake?
You're looking for the contentInset
property.
Also, you're re-declaring the variable insets
in your code. Consider changing to the following for simplicity:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
if([UIScreen mainScreen].bounds.size.width == 320)
{
return UIEdgeInsetsMake(0,12,20,2);
}
return collectionView.contentInset;
}
UIEdgeInsetsMake creating a weird band on the cell, and I don't know how to fix it
Great news: you're not going crazy. Your stretchable image code is likely perfect. The problem is that UITableView with a grouped style adds extra points to the height of your cells in addition to the value you return in heightForRowAtIndexPath:
So the solution to your problem is to return an adjusted cell height in heightForRowAtIndexPath:
based on the total number of rows in the section:
- (CGFloat)heightForRow:(NSIndexPath *)indexPath {
CGFloat standardHeight = 44.0f;
NSInteger numberOfRowsInSection = [self numberOfRowsInSection:indexPath.section];
if (numberOfRowsInSection == 1) {
standardHeight -= 2;
}
else if (indexPath.row == 0 || indexPath.row == numberOfRowsInSection-1) {
standardHeight -= 1;
}
return standardHeight;
}
Here's a sample image showing how UITableView does this:
As far as I know, only the grouped style table views are affected, and even then the effect changes based on the total number of rows in a given section:
- One Row Adds two points to the cell height (defaults height is 92 pixels on retina).
- Two Rows Adds one point to the height of the first and last rows (90 pixels high each by default on retina).
- Three Rows Adds one point to the height of the first and last rows (90 pixels high each on retina by default). The middle rows are unaffected.
This is so frustrating and has never been documented as far as I know. :-)
Update
The calculations above are for a grouped style table view using the default separator style of UITableViewCellSeparatorStyleSingleLineEtched
. Here's what to do in the other cases (i.e. UITableViewCellSeparatorStyleSingleLine
and UITableViewCellSeparatorStyleNone
):
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = 44.0f;
if (indexPath.row == 0) {
rowHeight -=1;
}
return rowHeight;
}
Understanding resizableImageWithCapInsets:UIEdgeInsetsMake?
It depends on your image, let's say you have 11pixels used as a border (left or top), a 1px central part and 11px used for the other border (right or bottom), you might wan't to use a 12.0, 12.0, 12.0, 12.0 UIEdgeInsets to preserve your borders from being distorted
UIButton UIEdgeInsets doesn't work
imageEdgeInsets are for positioning the image NOT the background image!
Use setImage
instead of setBackgroundImage
iOS UIBarButtonItem UIEdgeInsetsMake Not Affecting Size For Right/Left
You can customsize a view to initial the UIBarButtonItem,like this:UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:imageView];
so you can set the UIImageView size you want.
imageEdgeInsets' was deprecated in iOS 15.0
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)
Related Topics
How to Make a Swiftui Gesture That Keeps Running Code While the View Is Pressed
How to Run Timer Though the App Entered Background or Is Terminated
Get Uiscrollview to Scroll to the Top
How to Make Uibutton's Text Alignment Center? Using Ib
Xcode 11 Debugger Is Extremely Slow - a Known Problem
Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension)
Resignfirstresponder VS. Endediting for Keyboard Dismissal
How to Loop Over Struct Properties in Swift
Download Xcode 4.2 Documentation for Offline Install
How to Delete Object from Core Data in Swift 3
Xcode/Simulator: How to Run Older iOS Version
How to Convert Nsinteger to Nsstring Datatype
Disable Scroll on a Uiwebview Allowed
How to Detect Taps on Mkpolylines/Overlays Like Maps.App