i have a navigation bar, i want to change it using image, i was implement this code
self.navBar.layer.contents = (id)[UIImage imageNamed:#"flower.png"].CGImage;
but the background is still didn't change. the hierarchy of navigation bar are :
- UINavigationBar
- UINavigationItem
- UISegementControl
i already set an outlet in every part in that hierarchy..
is there something wrong???
Here's what I've used quite succesfully:
CALayer *backgroundImageLayer;
backgroundImageLayer = [CALayer layer];
backgroundImageLayer.frame = CGRectMake(0, 0, 300, 44);
backgroundImageLayer.backgroundColor = [UIColor redColor].CGColor;
backgroundImageLayer.contents = (id)[[UIImage imageNamed:#"barImage.png"] CGImage];
backgroundImageLayer.zPosition = -5.0;
[myNavigationConroller.navigationBar.layer addSublayer:test];
The key was setting the zPosition to -5.0, although any negative value should work. It causes the backgroundImageLayer not to interfere with UINavigationBar's buttons and label.
Related
I am working on a Layer-hosting View where I'm setting up a layer hierarchy as:
auto view = [NSView new];
view.layer = [CALayer new];
view.layer.masksToBounds = NO;
view.wantsLayer = YES;
and then using this later as follows:
auto rootLayer = view.layer;
auto layer = [CALayer new];
layer.frame = CGRectMake(0, 0, 100, 100);
layer.backgroundColor = rootLayer.backgroundColor;
[rootLayer addSublayer:layer]; //and similarly adding more sublayers
This works fine and I'm able to see all the layers on the UI. The problem arises when I try to apply a mask to this rootLayer
CAShapeLayer* maskLayer = [CAShapeLayer new];
maskLayer.path = getPath();//sets a CGMutablePathRef
rootLayer.mask = maskLayer;
After this none of the sublayers of rootLayer show up. I've even tried setting the frame of the maskLayer using
rootLayer.mask.frame = CGRectMake(0, 0, rootLayer.frame.size.width, rootLayer.frame.size.height);
but to no avail. I've tried setting the maskToBounds to NO for the rootLayer but it was already NO by default. I can't seem to figure out what's the issue here. Does setting a mask lead all the sublayers to be clipped? I didn't find this documented anywhere. From the documentation of mask:
An optional layer whose alpha channel is used to mask the layer’s
content.
So I interpreted that as to mean that it doesn't impact the sublayers.
I was trying to co-relate this to what's shown here in the Core Animation guide where despite setting the cornerRadius (a mask) to the parent layer, the child layers weren't clipped. But I'm not sure if the same applies when an explicit mask is set.
If I resize the window to be smaller than the metal view I can see the scrollbars for a second but I cannot click on them nor they stay visible. Do you know how I can change this behavior? I would expect the scrollbars to be visible and clickable as long as the window is smaller than the metal view.
nsview = gdk_quartz_window_get_nsview(window);
NSScrollView *scroll_view = [[NSScrollView alloc]initWithFrame: [nsview frame]];
[scroll_view setBorderType: NSNoBorder];
[scroll_view setHasVerticalScroller: YES];
[scroll_view setHasHorizontalScroller: YES];
[scroll_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[nsview addSubview: scroll_view];
self->clip_view = [[DvFlippedClipView alloc]initWithFrame: [nsview frame]];
[scroll_view setContentView: self->clip_view];
self->mtk_view = [[MTKView alloc]initWithFrame: [nsview frame]
device: self->device];
self->mtk_view.framebufferOnly = YES;
self->mtk_view.autoResizeDrawable = NO;
self->mtk_view.translatesAutoresizingMaskIntoConstraints = NO;
self->mtk_view_delegate = [[DvMetalViewDelegate alloc] init: self->mtk_view];
self->mtk_view.delegate = self->mtk_view_delegate;
[scroll_view setDocumentView: self->mtk_view];
From a different callback I do the following:
[self->mtk_view setBounds:NSMakeRect(0, 0, width, height)];
[self->mtk_view setFrame:NSMakeRect(0, 0, width, height)];
self->mtk_view.drawableSize = CGSizeMake(width, height);
I had a similar problem yesterday, and I suspect the strange scroller behavior and lack of scrolling altogether may be stemming from what I suspect your problem is. Hopefully it will at least enlighten your or someone else if you haven't already found an answer.
My view hierarchy looks like this:
The problem was that I had set MTKView as the view property of CenterTopViewController, rather than the Bordered Scroll View. Doing that, for all practical intents and purposes, removed MTKView from the hierarchy and set it so that its superview property pointed to the split view in which all of this resides. The scroll view did not seem to be part of the responder chain, and was never handling any scroll events (or at least not in any meaningful way).
Setting the scroll view as the view property of the view controller fixed everything.
P.S.
If you're confused as to why there is an unnecessary view above the MTKView, it's just a result of almost desperate attempts to uncover the problem before I figured it out. I haven't gotten around to fixing it yet.
I am looking into theming my app by setting window.appearance.
In my app, I draw some stuff inside layers. I also use Core Plot, which renders its charts in layers.
For the default aqua appearance, I just use the system colors (such as NSColor.textColor and NSColor.gridColor) and they are drawn in the correct color in CALayer. But changing the window's appearance to vibrant dark causes colors to be drawn incorrectly.
Is there any way to obtain the correct color for a givenNSAppearance? Private API is acceptable too.
If the question is not clear, here is a very simple example to show the problem.
I set up a CATextLayer that is added as a sublayer of the main view's sublayers and an NSTextFied that is added as a subview:
CATextLayer* textLayer = [CATextLayer new];
textLayer.string = #"Is this colored correctly? (Layer)";
textLayer.foregroundColor = NSColor.textColor.CGColor;
textLayer.contentsScale = 2.0;
textLayer.frame = (CGRect){0,0, [textLayer preferredFrameSize]};
NSTextField* textField = [NSTextField new];
textField.stringValue = #"Is this colored correctly? (View)";
textField.textColor = NSColor.textColor;
textField.font = (__bridge id)textLayer.font;
textField.editable = NO;
textField.selectable = NO;
textField.bezeled = NO;
textField.backgroundColor = nil;
[textField sizeToFit];
textField.frame = (CGRect){0, 60, textField.bounds.size};
[self.view.layer addSublayer:textLayer];
[self.view addSubview:textField];
On an Aqua window, both appear correctly:
However, on a dark vibrant window, the layer does not, while the text field does:
I'd like to know how to get the correct color for a given NSAppearance.
So I had an incorrect approach.
The right way to do it, is to implement -updateLayer in the view and take the colors' CGColor snapshot there. -updateLayer is called when the appearance changes, so the view can update it with the correct color values.
I'm using an NSPopover and I'm putting a NSViewController inside to be displayed as a custom view from a NSStatusItem. Most of the view controller displays correctly except for the NSButton that have a corner radius on them. There is some extra white leaking out where the rounded corners are being applied. Displaying the buttons within the actual app, this problem doesn't occur.
I feel it has to do something with the NSPopover appearance which I have set to "NSAppearanceNameAqua".
The NSButtons are within a NSView which are displayed in a NSTableView and are set to this style.
self.createdButton.wantsLayer = true
self.createdButton.layer?.backgroundColor = Utils.blackColor().CGColor
self.createdButton.layer?.masksToBounds = true
self.createdButton.layer?.cornerRadius = 5
The top image is when the actual app is open.
The bottom image is when the view controller is being shown within a NSPopover.
create corner radius using bezier path i.e.
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:view.bounds xRadius:3 yRadius:3];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = view.bounds;
maskLayer.path = path.toCGPath;
self.containerView.layer.mask = maskLayer;
I need to make a simple scroll view in xcode with width of 280 and height of 80 and with images inside thats scrolls horizontally. i want to make this programmatically.
I assume you mean the UIScrollview, which has a guide written by apple found here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
A guide that I personally used was this one:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
I'll take you through the quick basics of adding the scrollview to your view and adding images to it.
I'm guessing you're new to Objective C, so I'll give you a quick guide. Firstly, you'll want to make a UIScrollView object. This is done by declaring the following:
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0,0,320,250)];
You'll notice I set the frame. The first two numbers of CGRectMake give you the x and y origin of the point while the last two numbers are for how wide and tall you want your object to be.
Afterwards, you'll want to add images to it. You'll need a UIImageview.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
Note that I positioned the image at 0,0, giving it a height of a 250 and a width of 320. This ensures that it fills entire scrollview's initial view.
imageView.image = [UIImage imageNamed:#"foo.png"];
You'll attach an image to the imageView. But wait, there's more. So far you've created these objects but have not yet associated them with the view. So if we are in a ViewController class (you'll have to look up what that is), the ViewController contains a view. We can attach our objects to the view.
[aScrollView addSubview:imageView]; // Adds the image to the scrollview
[self.view addSubview:aScrollView]; // Adds the scrollview to the view.
If you want to add more images, you have to add them at different x origins. So our first added image was at 0,0. Our next added image should be at 320,0 (because the first image took up 320 pixels width).
UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 250)];
secondImageView.image = [UIImage imageNamed:#"bar.png"];
[aScrollView addSubview:secondImageView];
There are a number of options for scrollview that you will want to explore. The ones I found useful were:
aScrollView.delegate = self; // For gesture callbacks
self.pagingEnabled = TRUE; // For one-at-a-time flick scrolling
self.showsHorizontalScrollIndicator = NO; // Cleaner look for some apps.
self.alwaysBounceHorizontal = TRUE; // Look it up.