Set UIImageView with size 10x10 as background for UIView - ios

I have image with size 10x10 px and I need to set the image as background for UIView with the image copied horizontally and vertical, not stretched. Is it possible to do with ios?

you can do it like this:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bodyBG"]];
It copied horizontally and vertical in default.

Related

.scaleAspectFit - filling the blanks with black color

I have UIImages that I show in a UIImageView. The UIImageView has the
contentMode = .scaleAspectFit
property. The UIImages show just fine on the UIImageView, but I want to edit the UIImages so that they don't have any blank (transparent) space, and instead fill the blank spaces with a black color, and I want it to do this dynamically, depending on the end-users screen size.
Example:
What I want to do is replace the areas I've drawn in, with a black color, and save it as an UIImage.
Simply set backgroundcolor for the uiimageview
Set the background Color of the imageView to black
Using imageView outlet
self.imageView.backgroundColor = UIColor.black
Using IB
Set black color for imageview as well as set frame equal to frame of main view.
self.imageView.backgroundColor = UIColor.black
For AutoRisize:
self.imageView.frame = self.view.frame
For AutoLayout:
Set leading, trailing, top and bottom constraints with main view (self.view)

Change the size of the background image codewise from a label in swift

In the app that I am making I have some background images for some labels.
The image is 500x550 pixels but my label is 250x275 pixels.
How can I make the code change the width and height of the background so it fits the size of the label?
Code I have now:
lblMainNumber.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)
It is easier to have a container UIView with size of 250 x 275 that will contain 2 subviews with same size:
1) a UIImageView with your image (set its contentMode to UIViewContentModeScaleAspectFit)
2) UILabel with your text. It will be on top of the UIImageView to stay visible.
You would possibly want to set both subviews (via autolayout) to match the size of container view.
Pattern image approach is pain in the ass and not wort the effort.

Placing the button on top right of UIImageView

I have a UIImageView which is set to mode Aspect Fit. Now I want to place an edit button on top right corner of that image view. I am using auto layout and have a constraint of (Button->Trailing Space to superview) and the layout is connected via IBOutlet.
Once the image is set in UIImageView, I want to resize the ImageView Frame, so I can place the UIButton on top right. But UIImageView is not resizing as per image. Look at the gray part in the attached image (It is the background of ImageView)
imageView.clipsToBounds = YES;
imageView.image = image;
[imageView sizeToFit];
buttonLeadingSpace.constant = imageView.frame.origin.x+imageView.frame.size.width-44;
First of all, you should align your button with ImageView top & trailing. you can add constant=padding you need for your button.
UIImage is displaying as per your properties set for UIImageView (ie. contentMode = AspectFit). Here are the few options you might be interested in
If you want to fill the complete width just remove the height constraint from the UIImageView. It will fill the width and increase the UIImageView height as per image aspect ratio.
If you want same height and width of UIImageView set contentMode = aspectFit. this will fill the complete UIImageView but might clip the image.
Remove leading and trailing constraints from UIImageView. Align horizontal center and add image width constraint.

xcode UIView ScaleAspectFit for UITextfield setLeftView

I'd like to have an icon in my textfield and I'm using a the setLeftView attribute.
my code at the moment is
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 26, tb_teamA_playerA.bounds.size.height)];
leftView.contentMode = UIViewContentModeScaleAspectFit;
[leftView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed : #"person_26" ]]];
[tb_teamA_playerA setLeftView :leftView];
[tb_teamA_playerA setLeftViewMode: UITextFieldViewModeAlways];
that's the result.
the very left image is a separate UIImageView with the same image set and the scale set to ScaleAspectFit.
So I was expecting the two images to be the same size and scale but they are not?!
You are using UIViewContentModeScaleAspectFit, so the image will fit the size of the UITextField.
I think you should use UIViewContentModeScaleAspectFill, and if you have some problem with ths size and the two images still don't have the same size, you should resize you image.
The following is from the Doc
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
UIViewContentModeScaleAspectFit
Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

UIImageView overflowing its containing UIView after calling sizeToFit

My UIView (width: 352px) has a UIImageView subview (default width in Storyboard: 312px).
I want the UIImageView to adapt itself to the dimensions of the image it contains with the constraint that the width of the image view shouldn't exceed a maximal width size (in my case, 312px).
I set up the 'autosizing' configuration of my UIImageView to have a fixed left, top and right margin size. Nevertheless, when I call sizeToFit on my UIImageView and its image is larger than 352px, the UIImageView gets wider than its containing UIView.
Is there a convenient method to prevent such a behavior without doing the math based on the image dimensions? Am I using sizeToFit the right way?
Just use setClipsToBounds:
[imageView setClipsToBounds:YES];

Resources