Can't display image in custom keyboard - ios

i'm building custom keyboard for ios 8, so i want to display one image here but it's seems not work.
I really don't know why, here is my code:
var img = UIImageView()
img.frame = CGRectMake(0, 0, 50, 50)
img.image = UIImage(named: "interesting.jpg")
self.view.addSubview(img)
Nothing appear but if i change to display an UILabel, it's ok.
When i move this code to ViewController image display fine.
I couldn't display image in keyboard area only.

I think you could do this: UIImage(named: "interesting") and have the name of the image in the asset catalog be interesting

Related

How to set content mode when using FFImageLoading on button

I am not able to get the content mode working on button when I am setting the image using FFImageLoading library.I want to set the mode to ScaleAspectFill.
I tried setting the content mode of the button.imageview as well as the button itself.
button.ContentMode = UIViewContentMode.ScaleAspectFill;
button.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl(image_url).Into(button);
The image is not scaling and is appearing as ScaleAspectFit rather than Fill
Setting the button's ImageView's ContentMode will adjust the image's representation.
I used the code below to display two buttons. One has set the ContentMode and another doesn't:
UIButton btn = new UIButton(UIButtonType.Custom);
btn.Frame = new CoreGraphics.CGRect(50, 100, 100, 40);
btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn);
UIButton btn2 = new UIButton(UIButtonType.Custom);
btn2.Frame = new CoreGraphics.CGRect(50, 200, 100, 40);
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn2);
View.AddSubview(btn);
View.AddSubview(btn2);
They have different effects:
The first button doesn't have enough width to display this image so it clips the left and right edges due to the configuration of content mode. But the second button stretches the image to fit the whole space. Both buttons have the same size.

Swift - Issues with setting background image in a UIScrollView

I have created a UIScrollView and added UILabels and UIButtons. I then have created a UIImageView and added it to the my UIScrollView. I have used the following code to set the UIImageView to the background of my UIScrollView.
let paperImageView = UIImageView(image: UIImage(named: "White Notebook Paper.png"))
paperImageView.frame = CGRect(x: -60, y: -60, width: UIScreen.main.bounds.width + 60, height: currentY + 60)
scrollView.addSubview(paperImageView)
scrollView.sendSubview(toBack: paperImageView)
As you can see in the following screenshot, by UIImageView is distorted.
The background image supposed to look like notebook paper. As you can see, the spacing of the lines becomes wider the lower you go in the UIScrollView. Thanks for taking the time to share your thoughts.
I forgot to change the content mode of the image view.
paperImageView.contentMode = .scaleAspectFill

iOS Swift : How to create textfield with image

New to iOS.Learning iOS swift by blogs and online materials. I want to create login form which looks like below. Importantly icon before textfield. How can I achieve that ? Please suggest
And how can i add logo on whitespace top? do i need to add bar item and add picture/logo?
You can achieve in swift like below :
let leftImageView = UIImageView()
leftImageView.image = leftImage
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRectMake(0, 0, 30, 20)
leftImageView.frame = CGRectMake(0, 0, 15, 20)
textField.leftView = leftView
For Objective C try something like this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 26, 26)];
imgView.image = [UIImage imageNamed:#"img.png"];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
[paddingView addSubview:imgView];
[txtField setLeftViewMode:UITextFieldViewModeAlways];
[txtField setLeftView:paddingView];
From my understanding of your question, What i would suggest is the icon images you want to have on your interface. Open up the folder that has the xcode project in it. Click the top folder the one above the xcode project click assets. You know you are in the right folder when you get to the folder that has appicon folder in it. Put the images there. Then pull up your project next to the folder and dragg the items into your xcode project under the tab that says main storyboard then click the blue button in the bottom righthand corner now search your object library for image view drag that onto your interface then click the attributes inspector then about the top drop down add the image that you added before. Hope this helps.
you can create a nib with your image.
for example
let customNavBarView = NSBundle.mainBundle().loadNibNamed("SomeXibView", owner: self, options: nil)?.first as! UIView
self.navigationController?.navigationBar.inputView = customNavBarView
so far I've done this with mine and it works

What do I need for masking a UIImageView and how do I do it in Swift 3?

I was wondering what I would need if I wanted to use a mask image to get my UIImageView in a specific shape. From what I understand, to create a mask, I need to have an image with the shape of the mask all black on top of a white background. Something like this, for example:
First of all, is this sufficient to shape an image view, and if so, how do I do it in Swift 3? I can only find masking code that is either outdated or written in Objective-C. I've tried simply assigning the image above to an UIImageView and then assign the image view to the mask property of the UIImageView I want to shape, like so:
self.defaultImageView.mask = self.maskImageView
This didn't do anything. It just made self.maskImageView disappear (both image view's added through the storyboard and connected using IBOutlet properties). I'm sure I'm forgetting to do something. It can't be this simple. I would appreciate it if someone could help me out. Like I said, I put both image views on the exact same spot, on top of each other, in the storyboard.
UPDATE:
My first attempt to set the mask programmatically after deleting it from my storyboard.
let layer:CALayer = CALayer()
let mask:UIImage = UIImage(named: "Black-Star-Photographic-Agency")!
layer.contents = mask
layer.frame = CGRect(x: 0, y: 0, width: ((self.defaultImageView.image?.size.width)!), height: (self.defaultImageView.image?.size.height)!)
self.defaultImageView.layer.mask = layer
self.defaultImageView.layer.masksToBounds = true
The result was that the image view had completely disappeared and wasn't visible anymore. Am I doing something, am I forgetting something or both?
You should use a png image, which supports transparency, unlike jpg.
In Photoshop your image should look similar to this:
It doesn't matter if your shape is black or white. What matters is transparency of each pixel. Opaque area (black in this case) will be visible and transparent area will get trimmed.
Edit:
You should not create mask view from storyboard if you do so. It is not going to be a part of your view hierarchy. Just add it programmatically like this:
let maskView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
maskView.image = UIImage(named: "mask")
imageView.mask = maskView
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
maskView.frame = imageView.bounds
}
Output:
Here is a test project to show how it's working.
Also if you're using a custom frame/image and run into the mask not showing properly, try setting the content mode of the mask:
maskView.contentMode = .scaleAspectFit

How to make UIImageView automatically resize to the size of the image loaded

I have put an UIImageView control on my view with IB.
The size of the control is just something I decided upon, pretty random size really
What I want to do is the control to resize automatically whenever I set the image property to a new image. I want it to actually resize to the size of the image.
Can it be done automatically ? without any code intervention ?
If not - what will the best approach be in this case ?
What happens today is strange. I load images into the ImageView and I see the images getting displayed properly even though the size of the ImageView is not changed. This interferes with my intention of grabbing users touches over the ImageView. The user touches the actual image, but since some parts of the image are outside ( and this is the strange part ) of the ImageView - point mapping goes crazy
Can someone think of any explanation to this ?
thanks
The size of an image has no bearing on how large the UIImageView actually is, rather the size of the UIImageView solely depends on the size given to it in Interface Builder (or that you assigned to it). Else the images would be all whacky when you use the #2x images for Retina displays for example.
If you want to fix this, you must change the frame when setting the image as well. If you're doing this now:
[imageView setImage:[UIImage imageNamed:#"myImage.jpg"]];
change it to:
UIImage img = [UIImage imageNamed:#"myImage.jpg"];
[imageView setImage:img];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
img.size.width, img.size.height);
This will however not change the layout of view it is contained within, you can make it change the sizes of the other views automatically under iOS 6 using Layout Constraints. If you are an Apple Developer you can watch the WWDC instruction videos, they explain how that system works quite well.
If you're fine with the view not growing, and the problem is just how the image overflows it's bounds when you change it to one that does not match the dimension of the containing view, you can set the "Clip Subviews" checkbox in Interface Builder for the image view. This will make it so that the view will not draw anything outside it's own bounding box, if you also set the scaling mode to "Aspect Fill" or "Scale To Fill", the image will always fill up the entire bounds of the containing view.
Here is the code snippet I cut and paste often, using the same method as Hampus Nilsson above -
Example
func demo(x0:CGFloat, y0:CGFloat) {
let imgView = UIImageView(image: UIImage(named: "someImg.png"));
imgView.sizeToImage();
imgView.center = CGPoint(x:x0, y:y0);
}
UIImageView Extension
extension UIImageView {
/******************************************************************************/
/** #fcn sizeToImage()
* #brief size view to image
* ##assum (image!=nil)
*/
/******************************************************************************/
func sizeToImage() {
//Grab loc
let xC = self.center.x;
let yC = self.center.y;
//Size to fit
self.frame = CGRect (x: 0, y: 0, width: (self.image?.size.width)!/2, height: (self.image?.size.height)!/2);
//Move to loc
self.center = CGPoint(x:xC, y:yC);
return;
}
A wonderful cut & paste, use if needed!
let containerView = UIView(frame: CGRect(x:0,y:0,width:320,height:500))
let imageView = UIImageView()
if let image = UIImage(named: "Image_Name_Here") {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
}
}

Resources