I've created a simple view that will show some AVPlayer content, or if this does not exist (initial state) draw a splash image.
I've added the IB_DESIGNABLE directive, and am drawing the splash image in drawRect. However, in Interface Builder, the component renders as a black square. I'd like it to draw the image. Is this possible?
At runtime all works fine.
To investigate what goes on within the IB rendition for your drawRect: implementation, I would recommend to debug how your view behaves with IB using the debugger.
For that, select your view in IB, and then use the Debug Selected Views item from the Editor menu in XCode. By setting up a breakpoint in the drawRect: you should be able to understand what's going on and why you get a black square.
Just for reference, you can set custom variables in IB (such as your images) using the keyword IBInspectable, f.e. as:
IB_DESIGNABLE
#interface MyCustomControl : UIView
{
IBInspectable UIImage *myImage;
...
}
...
#end
For more info read this awesome blog where I found that keyword (I had been looking for something like that for quite a while).
Related
Im learning iOS. I've been reading Apple's View programming guide.
I did the followings to try out content mode:
I dragged a uiview into storyboard and made it my CustomView class.
Then i changed the content mode to be left and set the background color to be pink color.
Then there's a button that just simply changes the custom view's width and height to be 1.5 times bigger.
Now, i found an interesting thing:
I run this, and click the button, then:
Case 1: If i override the draw rect method
drawRect: overrided
Then the appearance of the view shifted down
Case 2: Without overriding the draw rect method
drawRect: not overrided
The size actually increased
After doing some search online and look into Apple's document, i couldnt any relavent things except one question mention that this might have something to do with drawLayer:inContext: behave differently if override the drawRect:
Does anyone know what is going on here?
(sorry about the formatting, this is a new account and i can't post more than 2 links.)
CODE:
For the CustomView, just either override drawRect or without it.
#import "CustomView.h"
#implementation CustomView
- (void)drawRect:(CGRect)rect{
//Do nothing, for case 2 i just commented out this method.
}
#end
For Changing the frame of customView:
- (IBAction)changeBound:(id)sender {
self.customView.frame = CGRectMake(self.customView.frame.origin.x, self.customView.frame.origin.y, self.customView.frame.size.width * 1.5, self.customView.frame.size.height * 1.5);
}
After doing some research, i think i figure out what happened:
So drawing background code is actually handled separately in -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context. That's why with empty drawRect, it still draws the background. Also, depending on the existence of drawRector not(Probably by calling respondsToSelector?), UIView behave different in drawLayer :inContext.
See Dennis Fan's answer in this link
So i've changed the background property of my default view in my storyboard file to as many as the colours in the rainbow. The colour shows up along with my buttons nicely in Xcode, but when I actually RUN the app, the background is always the same, ugly, default grey colour that you start with a new Xcode project ("Hello World!").
I've searched up for the answer all over Google and to no avail. Am I doing something wrong or is it just a glitch? It might be also important to mention than i'm using Spritekit, and that this "view" is the only view in my Storyboard file, the default one. Should I change self.view to skView? Would that work?
Oh and also, when I manually try to change the background colour under viewDidLoad in my .m class by doing:
self.view.backgroundColor = [UIColor backgroundColor];
the background STILL doesn't change and is always the same default colour. Please help me... i'm desperate right now... there's no answers anywhere to be found. Thanks.
All Storyboard, all graphical answer:
Explanation: You should modify the Main.storyboard, select the View Controller, change View in the Attribute Inspector, and apply a new Background color.
With exactly 0 lines of code, you should get this result:
Download project here.
I found myself in the same exact problem. After finding no help whatsoever online and exhausting myself trying different things; I decided to simply add a view to the storyboard and make it the background. This way you can change the background color and/or image and it will show up. It should be entirely constrained to the superview. I used the following constraints:
If you need to add other things to the storyboard (which it wouldn't really make any sense if you didn't) every other object should be contained in this view (it basically replaces the superview). This works well for me and I hope it can help someone else too.
Just try checking that you don't have a line saying 'self.view.backgroundColor = ...' inside of your .m file. i.e. somewhere in 'loadView:', 'viewDidLoad:' or 'viewWillAppear:'
(I first thought that you have linked to some different viewController inside of your storyboard, but if you have all your buttons displaying correctly, then this assumption is wrong)
If you just want a quick fix of the problem, then simply add a line in your .m file saying self.view.backgroundColor = [UIColor yellowColor];
The UIView.backgroundColor property is inherited by SKView, but is visible only if the SpriteKit view isn't rendering any SpriteKit scene.
Of course, in practice a SpriteKit view just about always has an SKScene assigned to it. When the view is rendering a scene, it paints itself with the backgroundColor of whatever scene it's assigned to render.
(This means that if you present a different scene, the background will change. It also means that if your scene's scaleMode and size are such that the scene doesn't fill the entire view, the view's backgroundColor will be visible in areas not covered by the scene.)
You can set an SKScene's background color in code using its backgroundColor property. If you create your scene graphically in an .sks file, you can set it in the inspector pane in Xcode:
IMPORTANT: This is the SpriteKit Scene inspector, seen when you edit a .sks file in Xcode -- not the view / view controller inspector that you see when editing a storyboard.
If you have the same problem as me! That you see the ugly gray background right after the SplashScreen, I have found a solution! Just after creating the skView, create an empty scene with a white background and present it directly!
let backgroundColoredScene = SKScene()
backgroundColoredScene.backgroundColor = SKColor.white()
skView.presentScene(backgroundColoredScene)
I had the same problem. Try to change the background color at main.storyboard as above, then restart Xcode.
The issue is with the SKView. A workaround I use is to change the SKView on the storyboard to a UIView. Then transition to an ViewControler with an SKView when you want to run your spritekit game.
Here's where you can change the view type on the storyboard
I made a Cocoa Touch Class called BlackCircle, a UILabel subview which called the drawRect() method to make a black circular label. I dragged a UILabel onto the LaunchScreen.xib file, and when I assigned the Cocoa Touch Class File as the class of the UILabel, I got an error-
"Launch Screen may not use instances of BlackCircle".
It works fine on the main.storyboard file, but it doesn't work on the LaunchScreen.xib file. How can I fix this? Thanks!
LaunchScreen will not load your custom instances. Remember the app hasn't launched yet. Thus you cannot have custom instances.
You are only allowed to use basic UIKit classes like UIView, UIImageView, UILabel, etc.
Your best bet would be to make an image resource for the black circular label and import it to the LaunchScreen.xib
Yes there is a way!
You can add an image view and use SF image like "circle.fill" (just enter the name in the topmost text field) and use tint color to style it in your way.
In my app, I would like to create a custom UIElement, in my case a UITextView with rounded corners and border color and everything. however I can only do it if I create the UITextView from code and add it to a view with the addSubView method.
That's not too elegant, plus it fails during the unit tests as well.
Is there a way how I can create my UITextView with using Interface Bulder and still being able to modify its layer property?
Any help is very much appreciated.
If you create an IBOutlet by ctrl-dragging from the nib or storyboard to your .h file, you can access your graphical element as usual with something like myTextView.layer.cornerRadius = 5.0;.
I want to make UITextview like in the Facebook comment view. How can I do it without using any external libraries?
Here is example image:
First, set the appearance of your UITextField to Alert to get the black keyboard.
Then, you only need two more views :
a black one for the background
a white one which will be the background of your text field, which you programmatically apply the following code to (don't forget to #import <QuartzCore/QuartzCore.h> first):
view.layer.cornerRadius = 10;
i have get sample code for this - here it is YIPopupTextView
What they do is use an image, that's it! They also probably expand the size of the UITextfield to make it fit the entire screen but yeah it's pretty easy. If you go into Interface Builder you can see a thing that says image. If you want to do this programmatically there is a value is textfield.background (A UIImage).
If you don't want to create an image you can create a custom UITextField subclass and use the -(void) drawInRect:(CGRect)rect function, and draw it with Quartz, and in interface builder set the UITextField class to your textfield.