How to override UIImageView's image property in an IBDesignable subclass? - uiimageview

I made a subclass of UIImageView which uses Core Graphics to generate a new image that is cropped to a circle with an optional border. It works fine when I run the app. In Interface Builder, however, the generated image renders properly, but it does so underneath the "no image set" placeholder for a UIImageView. Also, the image property shows up twice in IB, and the new image is only generated if I set the overridden field. If I set the image in the regular UIImageView field, it just acts as though it isn't subclassed. Is this just a bug in IB, or is there a fix?
#IBInspectable override var image : UIImage? {
didSet {
// Make the image a circle
makeCircleImage()
}
}

We are stuck with Apple's IB implementation, I'm afraid. I recommend that you find a workaround to overriding image. You can instead implement prepareForInterfaceBuilder and awakeFromNib and do it there, or have a method of doing this operation once in the draw or layout methods, or have a boolean IBInspectable var that does this operation when set.

Related

Resizing image to fit Ui Image View in xcode storyboard

I am trying to fit an image to fit the entire of the Ui Image View. I want to do this because I want to round my image and it does not work because the image is smaller than the entire view. So rounding it rounds the outside of the image and is not visible. I have tried to add constraints but that does not seem to have workout.
Here is an image of what I am trying to do:
https://i.stack.imgur.com/mIGow.png
The problem images:
https://i.stack.imgur.com/LGDyx.png
https://i.stack.imgur.com/K3RVZ.png
OK - the problem is that you don't understand the Xcode interface.
When you have added a UIImageView, set its image, and then select it, you see this:
If you click the image view again, Xcode shows an additional "highlight" outline:
That has nothing to do with how it will look when you run your app.
Regarding the screen-cap from the video you are watching... You either didn't notice, or forgot, two things:
it's a tutorial for SwiftUI - the screen-caps you've posted for what you're trying to do is from a UIKit based app.
at 17:20 of Part 1 of that series, the author explains how he is setting the corner radius.
To get what you want with UIKit, you need to write some code to give the image view rounded corners. To have it show up at design-time, you need to use an #IBDesignable image view subclass.
Here's a very basic example:
#IBDesignable
class RoundedImageView: UIImageView {
override func layoutSubviews() {
layer.cornerRadius = 20
}
}
If you set the Custom Class of your image view to RoundedImageView, and then either select either Editor -> Refresh All Views or have Editor -> Automatically Refresh Designable Views checked, you'll see this:

rounded button image - scaling doesn't looks good

I have an image of a button with rounded corners.
When I resize it (using auto-layouts) it doesn't looks so good.
If I'm trying to slice it (via xcode assets) it's shape looks better, but since the button image has some types of green, the slicing not working so good (you can see color differences when it's sliced).
Any other solution to be done here?
You can create a custom button class with rounded corners
class GameButton: UIButton
Then in layoutSubviews
override func layoutSubviews() {
self.layer.cornerRadius = 5
}
Try to set the CornerRadius to button layer and use that image. That could do better. Don't resize the image.

Hide NSView overflow

I'm a web dev guy. The overflow:hidden CSS property tells the render engine to not draw the current view's content across the parent's borders.
In my current project, I have a custom NSWindow with a custom NSView with rounded corners by using NSMakeRect, overwriting drawRect: and so on. A WebView inside the NSView is strechted across the entire NSView frame.
Now the WebView 'overflows' the rounded corners of theNSView`. What I do like to have is that the WebView has the same mask as the NSView.
How would you do that?
Also make sure your view sets clips subviews to true. As far as I understand you, this is what you are looking for, and prevents f.ex. images to reach out of the parent view.
[self.view setClipsToBounds:YES];
You can set a corner radius to the layer of a view.
myView.layer.cornerRadius = 5.0f;
You'll have to add QuartzCore.framework.

iPhoto like NSButton

How can I create a iPhoto like button like in the picture
I've tried out several things using round textured buttons or setting the button's image as template. But none of these approaches really works.
Thanks so far for your answers.
EDIT:
The image of the button should just be a simple pdf. The gradient and the white shadow should be drawn automatically.
As Justin mentioned you should create an NSButton with type Momentary Change. You should then indicate that it is a template image by including the suffix Template in the filename, e.g. EditTemplate.png.
I know you said you tried setting the image as template, but I found that this wasn't always effective if I wasn't using the Momentary Change button type.
You could subclass NSButtonCell. And implement these functions:
- (void)drawImage:(NSImage*)image
withFrame:(NSRect)frame
inView:(NSView*)controlView
{
// Draw the image of the pen.
}
- (NSRect)drawTitle:(NSAttributedString*)title
withFrame:(NSRect)frame
inView:(NSView*)controlView
{
// Draw the text.
}
- (void)drawBezelWithFrame:(NSRect)frame
inView:(NSView *)controlView
{
}
To setup the subclass just edit the "Custom Class" field of the Button Cell in Interface Builder from "NSButtonCell" to "YourSubClass"
I think the simplest solution is to create button with type: Momentary Change and add on it image with transparent background with .png type of file and when button is ON You can change to other picture with background.
For example to set/change image:
[_yourButton setImage:_image];
If I have understood correctly what You want to do.

UIView backed by CATiledLayer in UIScrollView doesn't scroll initially

I'm adding a CATiledLayer backed UIView in UIScrollView.
When the view is first loaded, I'm trying to fit the UIView, by setting the zoomScale of UIScrollView - this fits the UIView and the layered contents.
I'm having a method to fetch the tiles of image and I'm rendering them in drawLayer:inContext:
Now even if the contentsize of scrollview/frame of CATiledLayer view is greater than UIScrollView, it doesn't scroll initially.
The moment I try to zoom by pinching the screen, I'm able to scroll perfectly.
I can't scale the CGContext in drawLayer:inContext, since the context I receive is of a tile and not whole image and I have 20 tiles which make up my image.
At the end of the initWithFrame of the PDFScrollView i added the line:-
self.zoomScale = 1.0;
This worked for me.
The code is pretty much doing the same stuff as apple example
http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40010281-Intro-DontLinkElementID_2
which also doesnt scroll on loading

Resources