Is it possible to add an imageView behind a UITableView? How would I add this image:
So that users could see it behind a tableview (background set to clear)?
The following should work:
tableView.backgroundColor = UIColor(patternImage: UIImage(named: "your_image_name")!)
As the function names imply, a UIColor is created from the image you supply.
Refer to the Apple documentation here for more information.
If you go into the Storyboard, you can insert the Image View, insert the image in, then in the Document Outline, you can move the image above the table and it'll go behind it. Then set the tableView alpha in the inspector as .5 or however opaque you want it.
Funny, I just did a tutorial that had this in it, but code was used instead of an image. In Swift 2.0 you create a file (let's call it BackgroundView) of type UIView, add this code in the pre-made drawRect function:
// Change these colors to the ones you want.
let lightPurple: UIColor = UIColor(red: 0.377, green: 0.075, blue: 0.778, alpha: 1.000)
let darkPurple: UIColor = UIColor(red: 0.060, green: 0.036, blue: 0.202, alpha: 1.000)
let context = UIGraphicsGetCurrentContext()
//// Gradient Declarations
let purpleGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [lightPurple.CGColor, darkPurple.CGColor], [0, 1])
//// Background Drawing
let backgroundPath = UIBezierPath(rect: CGRectMake(0, 0, self.frame.width, self.frame.height))
CGContextSaveGState(context)
backgroundPath.addClip()
CGContextDrawLinearGradient(context, purpleGradient,
CGPointMake(160, 0),
CGPointMake(160, 568),
[.DrawsBeforeStartLocation, .DrawsAfterEndLocation])
Then create a configureView function in the TableViewController and put this line of code in it:
tableView.backgroundView = BackgroundView()
Then call the function in the viewDidLoad.
Related
I have a collection view that has its edges constrained to the safe area. However, the collection view fully covers the entire screen. Now if I change the contstraints remove the top constrain and change it to a fixed height constraint, the collection view STILL ignores it and fills the whole screen. When i remove all constraints, the collection view does not fill the whole screen. This error only affects the top constraint, all other constraints work fine.
UPDATE:
when I comment out some lines in the headerView supplementary view delegate function, it works again:
func pressedColour(colour: UIColor){
let origImage = self.imageView?.image
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
self.setImage(tintedImage, for: .highlighted)
//self.imageView?.contentMode = .scaleAspectFill
self.tintColor = colour
}
The lines I comment out call that function. The autolayout error is
2018-09-13 23:38:16.556318+0100 DoppelChat[59765:15398941] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60c00028a780 UICollectionView:0x7fac548e6600.top == UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide'.top (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x60c0004892e0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x7fac59a7e150.minY == 0 (active, names: '|':_UIParallaxDimmingView:0x7fac59a7eb90 )>",
"<NSLayoutConstraint:0x60c00048b4a0 'UIViewSafeAreaLayoutGuide-top' V:|-(64)-[UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7fac59a7e150 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60c00028a780 UICollectionView:0x7fac548e6600.top == UILayoutGuide:0x60c0001bea00'UIViewSafeAreaLayoutGuide'.top (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
I have narrowed it down to this line causing the issue:
let origImage = self.imageView?.image
ok, well I solved it, if it means anything to you, I used this function for button pressed colour instead:
func pressedColour(colour: UIColor, uiImage: UIImage?, alphaMultiplier: CGFloat){
let origImage = uiImage
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
self.setImage(tintedImage, for: .highlighted)
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
colour.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
self.tintColor = UIColor(red: red, green: green, blue: blue, alpha: alpha * alphaMultiplier)
}
and the uiImage argument i passed in was the image literal not the button.imageview.image
I found this code part on GitHub and it works really fine, I can even change the color by clicking on the color cube. Also, I could copy the color cube and use anywhere I wanted. I want to know how to initialize this in Swift 3.
I tried this but couldn't figure it out.
let cc = [UIColor(red: CGFloat(160/255), green: CGFloat(183.0/255), blue: CGFloat(227.0/255), alpha: 1), UIColor(red: CGFloat(160/255), green: CGFloat(183.0/255), blue: CGFloat(227.0/255), alpha: 1)]
You should use color literals in order to achieve this.
#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
In fact, you shouldn't even memorize this, as the autocompletion will show a Color Literal option to you if you start typing it in.
After that, double-click on the appearing square and you can pick from colors there:
Clicking on Other... reveals the standard color picker of macOS:
If you are creating color literals in one of your model files, do not forget import UIKit before you do this.
Does anyone know how to create the following? I cannot find any tutorials.
I can get the effect without a slider, but it is not as smooth. Or I can incorporate the slider but not update the picker with the value as it expects CGPoints instead of Ints and expects it in the gesture.locationInView(colorWheel) format, which a standard CGPoint will not accept.
You can use Cocoa controller for this. There are so much of controllers which is similar to this interface.
https://www.cocoacontrols.com/controls/hsv-color-picker
hope this link will help you.
I was able to just use the color wheel as a background and used the slider value to change the hue
func changeSet(changeValue: CGFloat) {
var value = (360 - currentAngleGlobal) / 360 // I had to deduct from 360 as my values were inverted
var color = UIColor(hue: CGFloat(value), saturation: 1, brightness: 1, alpha: 1)
self.colorOutput.backgroundColor = color
}
How can I create a button with an image so I can still decided the CGSize myself. Right now I can only do this.
let playNode = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
playNode.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
playNode.name = "play"
addChild(playNode)
I would like to replace the red color with an actual image. So far I haven't found a way how to actual create a button with an image AND decide its CGSize. I do know how to create a button just with an image, but I can't determine its CGSize then. Any help would be appreciated !
You can set the size property of your node after you've set the image. Like that:
let playNode = SKSpriteNode(imageNamed: "yourImage")
//Set it after you've set the image.
playNode.size = CGSizeMake(200, 200)
playNode.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
playNode.name = "play"
addChild(playNode)
Working with my app, i encountered some issues with drawing stuff:
I was wondering why, back to Obj-c, -moveToPoint() and -lineToPoint() were drawing everything with no problem and now, with swift, everything seems the same except for a strange border appearing on my view. Let me explain better:
Imagine that your task is to draw a basic line from A(0,0) to B(10,10)
We all know how to make that in obj-c, but in swift there is something new to me:
var path : NSBezierPath = NSBezierPath(rect: dirtyRect)
let color = NSColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
color.set()
path.moveToPoint(NSPoint(x: 0,y: 0))
path.lineToPoint(NSPoint(x: 10,y: 10))
path.lineWidth = 5.0 //Makes the line 5px width, but it even
//creates an annoying border along the view
//Try it out, i can't figure out how to get rid of it
path.stroke()
You are initializing the bezier path with a rect, so that rect is part of the path that gets stroked when you call path.stroke(). You can just initialize the path as NSBezierPath().
I suggest, instead of using NSBezierPath, NSColor, and NSPoint, you should use the updated UIBezierPath, UIColor, and CGPoint respectively.