In a case of a custom view, the only way i can think of is to lay an UIImageView on top is it and set it's background to some image.
Can image be overlaid onto the regular UIView?
I prefer to place an UIImageView covering whole UIView and arrange it to back in Interface Builder. I think it is the most convenient way to do it.
You can add image as background with image pattern. Use the following line of code.
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"YouImageName.png"]]];
In case you are not applying it to self.view, but to some other view in you xib file, then you need to make it IBOutlet first.
Related
I'd like to change the color of all of my NSToolbarItem's labels.
I need to set it to white because it suits better to the background color of my NSWindow, but it defaults to black and I haven't found a way to change it neither in Interface Builder nor directly by code (NSToolbarItem implements setLabel, but it just sets the text string).
If possible, I'd like to avoid:
Replacing the whole NSToolbar by a custom NSView. Would feel like reinventing the wheel to me.
Having to create custom NSViews inside NSToolbarItem. It would imply having to leave blank all of its labels and adding the white-colored label inside the custom view.
In case anyone is interested, I solved it by:
Using custom views inside NSToolbarItems containing both a button and a label.
Displaying icon only instead of icon + label in NSToolbar in order to hide the default label.
I had to deal with another problem related to a bug with Interface Builder: the custom view was not showing at all. I was able to fix it thanks to this answer.
You can change it using NSMutableAttributeString.
For example:
-(void) awakeFromNib{
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.label];
NSRange titleRange = NSMakeRange(0, title.length);
[title addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:titleRange];
[self setLabel:title]; }
You have to subclass NSBarButtonItem and override the drawRect method for that. Otherwise it will use [NSColor controlTextColor] or [NSColor disabledControlTextColor].
Is it a chance to programatically change the background color of NSCollectionView?
I was trying subclassing.. but not working..
#interface CollectionViewBg : NSCollectionView
in .m
[self setBackgroundColors:[NSArray arrayWithObjects:[NSColor blueColor], nil]];
In .m, remove this line :
[self setBackgroundColors:[NSArray arrayWithObjects:[NSColor blueColor], nil]];
And use this code:
- (void)drawRect:(NSRect)dirtyRect{
[[NSColor blueColor] setFill];
NSRectFill(dirtyRect);
}
Also don't forget to change class of NSCollectionView object in IB to CollectionViewBg.
Hope this helps :)
Guess subclassing isn't a great idea, because most of Cocoa controls provide various techniques to avoid subclassing in order to customize appearance and behavior.
In this particular case, you can set backgroundView property to your NSCollectionView and provide a custom view that is dedicated for drawing a custom background.
If you need to change only background color, then you can consider take advantage of the fact that collection view usually resists inside scroll view. Just set backgroundColor and drawsBackground properties of NSScrollView.
Here's a more modern solution. First, make yourself an NSView subclass that draws the effect you want to achieve. Here's a simple one that paints a block colour:
class ColouredView: NSView {
#IBInspectable var color: NSColor = .windowBackgroundColor
override func draw(_ dirtyRect: NSRect) {
color.setFill()
dirtyRect.fill()
}
}
Now go to your nib or storyboard and drag a custom view onto the left bar. We don't want it to be part of the view hierarchy, but we do want it to be listed under the view controller containing the collection view. Just drag it in below 'First Responder'. You can then set up any important properties or relationships - in my case, the background colour.
Finally, right-click your collection view and connect its 'background view' outlet to your custom view. This lets you do all the setup in your storyboard. If you like you can make your custom view #IBDesignable but I find that usually causes more trouble than it's worth.
For an iPad app I am writing I have a container UIView with two subview that are UIView subclasses:
A UIImageView whose image has a portion of it cut away to reveal what is below it.
A UIButton below the UIImageView that is revealed through the cut away portion of the UIImageView.
Since the UIImageView overlaps the UIButton spatially it is preventing touches from reaching the UIButton even though the UIButon is fully visible due to the alpha matte cutout in the UIImageView. How do I allow the UIImageView to pass touches to it's sibling UIButton?
Thanks,
Doug
UIImageView usually won't block touches, UIViews do.
You can set the userInteractionEnabled property on the overlapping views to NO, then touches should go through them.
An other approach would be writing a custom hitTest that redirects the thouches to the button.
In addition to Bastian's answer it was also necessary for me to uncheck the Opaque drawing attribute in interface builder for my UIImageView
Even with user interaction enabled, which is the default value when placing a UIImageView in Interface Builder, the touches should pass through to your button underneath, even if your image view has a solid background. Something else must be going on like a UIView sitting on top of the button.
If you are trying to do something more complex to get touches to pass through to underlying views or a separate view controller whose view is underneath, I created this simple open source library:
https://github.com/natrosoft/NATouchThroughView
The REAMDE and demo show how to use it.
Ok, so I've created an image in Photoshop that will align with the buttons on my app, and now I'd like to make it the background image of my window so that the characters on the image will correspond to the keys on my app (a small calculator demo app I've been working on)
Basically, instead of giving buttons Text like 1,2,3,4, etc. I've made a 3x3 map with numbers of a different font just because it will look pretty
What I'm having difficulty with now is that I can't seem to make the image the background of my window.
I created an NSImageView and I dragged the image file onto it, so I can see it now, but I can't make it the background.
Do I need to subclass the NSImageView or is there some simple method?
I'm using XCode 4, btw
Thanks!
I think you should be able to do something like:
[window setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:#"myImage.png"]]];
This is something that a layer-backed window would be good at:
[[window contentView] setWantsLayer:YES];
[[window contentView] layer].contents = myImage;
I think you stand a better chance of getting this to resize sensibly (assuming you need to) than with a pattern color.
Just to give you more options -- you should be able to [window setContentView:myImageView]. In the .xib file you'd want to add your buttons etc. as subviews of the image view.
I don't necessarily recommend this approach, but it's something to think about.
I created UIImageView with the help of Interface Bulder. Now I want to place label inside it (as its subview). In code I can type something like: [myUIImageView addSubview:myUILabel]; But can I do it with the help of IB? I found the solution for UIView, but can't find something similar for UIImageView.
You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome.
So there is an easy workaround. Instead of dragging an instance of UIImageView in the nib, just drag a UIView and change its class to UIImageView from UIView (cmd+4 option of inspector). The only difference you find in the nib for default imageView instance and your new UIImageView subclass instance is: you cannot set image to your new imageView from nib (cmd+1 option). So, in the -viewDidLoad method of its appropriate viewController, set image to this outlet of UIImageView.
By doing so, you are free to add subviews to your "now UIImageView" instances in interface builder, which is much easy.
I would like to add answer.
While it sucks you cannot add subview to UIImageView, you can get the same effect by incorporating UIView with transparent (clear color) background.
Then put the UIImageview BEFORE it.
So the UIView has the subviews and the UIImageview is the background of the UIView.
I think that's apple's intent.
Here is a screenshot:
Here is the result:
Don't forget to set background as clear color
Now if someone could actually point me to a tutorial how to do this it'll be great. I spent hours doing it the checked answered way. The checked answer is a fine answer but very unintuitive because you can't see your background image clearly while working. I think mine is the proper way to do so.
In latest XCode(4.5) there is an option to drag and drop the required controls to the parent.
It is quite easy.
Attached screen shot for the same. I dragged the Label/TextField and Button to UIImageView
Use this code:
UIImage *image = [UIImage imageNamed:#"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view insertSubview:imageView atIndex:0];
(replace background.png with image) (replace atIndex:0 with whatever place in the .index root you want to insert the image and your off.