Uiview Background Color in Swift

UIView background color in Swift


self.view.backgroundColor = UIColor.redColor()

In Swift 3:

self.view.backgroundColor = UIColor.red

How to split the background color of UIView in Swift?

You should create a custom UIView class and override draw rect method

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(HorizontalView(frame: self.view.bounds))
}
}
class HorizontalView: UIView {
override func draw(_ rect: CGRect) {
super.draw(rect)

let topRect = CGRect(x: 0, y: 0, width: rect.size.width/2, height: rect.size.height)
UIColor.red.set()
guard let topContext = UIGraphicsGetCurrentContext() else { return }
topContext.fill(topRect)

let bottomRect = CGRect(x: rect.size.width/2, y: 0, width: rect.size.width/2, height: rect.size.height)
UIColor.green.set()
guard let bottomContext = UIGraphicsGetCurrentContext() else { return }
bottomContext.fill(bottomRect)
}
}

Sample Image

Can't set UIView's background color Swift 5

view needs a height

view.heightAnchor.constraint(equalToConstant:200).isActive = true

OR For label height plus top and bottom padding 40

title.topAnchor.constraint(equalTo:view.topAnchor, constant:20).isActive = true

UIView with background color looks different than CALayer with background color. Why, and how to make them match?

You have in fact given them different colors.

  • The layer's color is set in code and is UIColor.blue.cgColor.

  • The view's color is created in the storyboard and is

      R:0.02 G:0.2 B:1 A:1

    as revealed by examining it in the view debugger.

Why? Because you have failed to take into account the storyboard color's color space. It is Generic RGB, whereas the color you create in code is Device RGB, which is sRGB.

Another way to see this is to do some logging. In the running app, the two colors are

<CGColor 0x60000296aa60> 
[<CGColorSpace 0x60000296ab20>
(kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)]
( 0 0 1 1 )>

and

<CGColor 0x600002976940> 
[<CGColorSpace 0x60000296ab20>
(kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)]
( 0.01675 0.198341 1 1 )>

As you can see, the second color has been converted to sRGB color space, which is not the same as the Generic RGB you started with, and thus we get a different color after the conversion.

and what would I change to make them look the same

If you set the storyboard color's colorspace to Device RGB and then set the sliders to give a simple blue, you'll end up with the same colors in the running app.

Change UIView background color in SwiftUI

Hope this will help to understand:

var body: some View {
Color.purple
.overlay(
VStack(spacing: 20) {
Text("Overlay").font(.largeTitle)
Text("Example").font(.title).foregroundColor(.white)
})
.edgesIgnoringSafeArea(.vertical)
}

Another If you use the views in Group

var body: some View {
Group {
Text("Hello SwiftUI!")
}
.background(Color.black)
}

Change background color of custom UIView

I checked your code your have to set Stack like that so that custom-view have stack-frame

       stackView.distribution = UIStackViewDistribution.fill
stackView.alignment = UIStackViewAlignment.fill

viewDidLoad

override func viewDidLoad() {
let incidentView = WelcomeScreenButtonView(text: "Hasar\nAnında", imageName: "hasar")
stackView.axis = UILayoutConstraintAxis.horizontal
stackView.distribution = UIStackViewDistribution.fill
stackView.alignment = UIStackViewAlignment.fill
stackView.spacing = 0.0
stackView.addArrangedSubview(incidentView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.anchor(nil, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 90, rightConstant: 0, widthConstant: 0, heightConstant: 90)

}

When use cornerRadius, iOS UIView background color is wrong color

OK - what you're seeing is "artifacts" of anti-aliasing of the border... because the border is draw on the inside of the view.

It's been this way since (I believe) the first version of iOS.

The artifacts are even more noticeable when using a corner radius to make the view round:

Sample Image

and zoomed-in to 200%:

Sample Image

The way to avoid this is to use a CAShapeLayer with a rounded-corner UIBezierPath.

Here's a quick example view subclass, with public properties similar to what you're already using on the .layer:

class ExampleView: UIView {
public var fillColor: CGColor = UIColor.clear.cgColor {
didSet {
shapeLayer.fillColor = fillColor
}
}
public var strokeColor: CGColor = UIColor.clear.cgColor {
didSet {
shapeLayer.strokeColor = strokeColor
}
}
public var lineWidth: CGFloat = 0 {
didSet {
shapeLayer.lineWidth = lineWidth
}
}
public var cornerRadius: CGFloat = 0 {
didSet {
shapeLayer.cornerRadius = cornerRadius
}
}
private var shapeLayer: CAShapeLayer!
override class var layerClass: AnyClass {
return CAShapeLayer.self
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() -> Void {
shapeLayer = self.layer as? CAShapeLayer
shapeLayer.fillColor = fillColor
shapeLayer.strokeColor = strokeColor
shapeLayer.lineWidth = lineWidth
}
override func layoutSubviews() {
super.layoutSubviews()
let pth = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
shapeLayer.path = pth.cgPath
}

}

That gives us:

Sample Image

and, again zoomed-in to 200%:

Sample Image

Here's an example view controller showing the differences:

class ExampleViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .black

let test = UIView()
test.backgroundColor = .white
test.layer.cornerRadius = 7
test.layer.borderColor = UIColor.black.cgColor
test.layer.borderWidth = 2


let circleTest = UIView()
circleTest.backgroundColor = .white
circleTest.layer.cornerRadius = 20
circleTest.layer.borderColor = UIColor.black.cgColor
circleTest.layer.borderWidth = 2


let myTest = ExampleView()
myTest.backgroundColor = .white
myTest.cornerRadius = 7
myTest.strokeColor = UIColor.black.cgColor
myTest.lineWidth = 2


let myCircleTest = ExampleView()
myCircleTest.backgroundColor = .white
myCircleTest.cornerRadius = 20
myCircleTest.strokeColor = UIColor.black.cgColor
myCircleTest.lineWidth = 2


view.addSubview(test)
view.addSubview(circleTest)
view.addSubview(myTest)
view.addSubview(myCircleTest)

test.translatesAutoresizingMaskIntoConstraints = false
circleTest.translatesAutoresizingMaskIntoConstraints = false
myTest.translatesAutoresizingMaskIntoConstraints = false
myCircleTest.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
test.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
test.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
test.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -170),
test.heightAnchor.constraint(equalToConstant: 40),

circleTest.topAnchor.constraint(equalTo: test.bottomAnchor, constant: 8),
circleTest.centerXAnchor.constraint(equalTo: view.centerXAnchor),
circleTest.heightAnchor.constraint(equalToConstant: 40),
circleTest.widthAnchor.constraint(equalTo: circleTest.heightAnchor),

myTest.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
myTest.topAnchor.constraint(equalTo: circleTest.bottomAnchor, constant: 8),
myTest.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -170),
myTest.heightAnchor.constraint(equalToConstant: 40),

myCircleTest.topAnchor.constraint(equalTo: myTest.bottomAnchor, constant: 8),
myCircleTest.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myCircleTest.heightAnchor.constraint(equalToConstant: 40),
myCircleTest.widthAnchor.constraint(equalTo: myCircleTest.heightAnchor),

])
}

}


Related Topics



Leave a reply



Submit