How can I set UIView background in Swift? - ios

I am trying to set the background color of UIView programmatically. The code I am using is
UIView.appearance().backgroundColor = .blue
It works fine when I open the app, but as soon as I interact with it, the entire screen becomes a block of the selected color:

first of all you have to give this view an outlet for example:
#IBOutlet weak var myView: UIView!
Then you can use this:
myView.backgroundColor = UIColor.blue
but the previous should be written in your
viewDidLoad()
so finally your code will be like that:
#IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
myView.backgroundColor = UIColor.blue
}

Related

swift UIVisualEffectView with mask autolayout varies by iOS device

I am working with swift UIVisualEffectView with mask option.
I am trying to make a blurred UIVisualEffectView masked by UIImageView.
I've set autolayout constraints to the views at the storyboard but the result varies through the iOS device.
Storyboard, code and results are attached below.
I would be glad if anyone help me.
I've set all auto layout constraints at the storyboard.(Capture included below)
Code is very simple. I've only set blurView.mask = topImageView.
class TestViewController: UIViewController {
#IBOutlet weak var bottomImageView: UIImageView!
#IBOutlet weak var topImageView: UIImageView!
#IBOutlet weak var blurView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
blurView.mask = topImageView
}
// Still same issue when I move the mask to 'viewDidLayoutSubviews()' method
//
// override func viewDidLayoutSubviews() {
// super.viewDidLoad()
// blurView.mask = topImageView
// }
}
Storyboard and Code capture
Results depending on iOS versions
---
I've tried to move mask code from viewDidLoad() method to viewDidLayoutSubviews() but it's still same.
Self-solved by the below code.
Received a hint from similar issue : 'https://stackoverflow.com/questions/58998541/swift-mask-uiview-constraints-issues/59037761'
override func viewDidAppear() {
self.setupBlur()
}

Scroll View User Interaction Disabled Swift 4

I have a swift project, and I created the following layout structure:
And it looks like this:
Everything's fine, BUT: I don't know why the scroll view is not working. And I can't do user interaction besides the play button at the bottom.
Here is my swift file:
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.isScrollEnabled = true
scrollView.isUserInteractionEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height+300)
}
What should I do?
I have the same problem on another view, where I have a WebView as the main part of the screen.
Thank you.

UIView doesn't center properly in UIScrollView with autolayout

I'm making an application where I need an x amount of custom UIView and place them in a scrollView. The layout of the scrollView and the UIView xib are set with AutoLayout. The result I'm getting now is this:
First View is well centred in ScrollView
Second View is wrong and has a lot of space in between
See my VC Code under here.
let sponsors = Sponsors.createSponsors() <-- Array
override func viewDidLoad() {
super.viewDidLoad()
configureSponsors()
}
//MARK: Load the AddView in ScrollView
func configureSponsors() {
self.scrollView.contentSize = CGSizeMake(CGFloat(sponsors.count) * self.scrollView.frame.size.width, self.scrollView.frame.size.height)
for sponsor in sponsors {
numberOfItems++
let addView = NSBundle.mainBundle().loadNibNamed("AddView", owner: self, options: nil).last as! AddView
addView.addDataToAddView(sponsor)
addView.frame = CGRectMake(CGFloat(numberOfItems - 1) * scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)
self.scrollView.addSubview(addView)
}
}
And here is my UIView code:
//MARK: Outlets
#IBOutlet weak var backgroundImageView: UIImageView!
#IBOutlet weak var sponsorTitle: UILabel!
#IBOutlet weak var sponsorLogo: UIImageView!
#IBOutlet weak var sponsorSubtitle: UILabel!
#IBOutlet weak var roundView: UIView!
#IBOutlet weak var playMusicButton: UIButton!
//MARK: Properties
var cornerRadius: CGFloat = 3.0
func addDataToAddView(sponsor: Sponsors) {
backgroundImageView.image = UIImage(named: sponsor.backgroundImage)
sponsorLogo.image = UIImage(named: sponsor.logoImage)
sponsorTitle.text = sponsor.title
sponsorSubtitle.text = sponsor.subTitle
}
override func layoutSubviews() {
roundView.layer.cornerRadius = roundView.frame.size.width / 2
roundView.alpha = 0.7
roundView.clipsToBounds = true
}
//MARK: PlayVideo
#IBAction func playVideo(sender: UIButton) {
//play music
}
I've already searched for the same problems. I found out that I have to use the Pure Auto Layout Approach. Should that mean I need to programatically set the constraints for the UIView?
Many thanks,
Dax
Update:
Pictures for scrollView setup:
You should not perform layout calculations in viewDidLoad as frames are not set correctly till then.
Correct place to do this is either viewWillLayoutSubviews or viewDidLayoutSubviews as you will get the correct frame data when these functions are called

ScrollView unable to display image which passed from another VC

I need to display the image pass in from the another ViewController
first,I have tested using only ImageView to display the image and it works as below:
Thanks. Your help is greatly appreciated.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var TempImgView: UIImgeView!
var passInImg: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
TempImgView.image = passInImg!
}
When I use UIScrollView ( for scrolling the large passInImg) , below code not working
Probelm:
error : fatal error: unexpectedly found nil while unwrapping an
optional value
But I have tested the pass in image in the above code.
what need to be done with the scrollView?
Should be used in viewDidLoad?
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var myUIScrollView: UIScrollView!
var myImgView: UIImageView!
var passInImg: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
self.myUIScrollView.maximumZoomScale = 10.0
self.myUIScrollView.minimumZoomScale = 1.0
self.myUIScrollView.delegate = self
myImgView.image = passInImg!
self.myUIScrollView.addSubview(myImgView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return myImgView
}
First check if myUIScrollView is connected to an outlet in storyboard and always check before using an optional variable:
if let img = passInImg {
myImgView.image = img
}
The difference is the way you are declaring the UIImageView.
In the first case:
#IBOutlet weak var TempImgView: UIImageView!
TempImgView is an #IBOutlet and hence initialised whenever the storyboard view/xib is loaded.
But then when you are using the scrollview, you have declared the UIImageView as a class variable and not #IBOutlet.
var myImgView: UIImageView!
It has to initialised before you try to access it.
Try:
myImgView = UIImageView()
myImgView.image = passInImg!
Please initialize the myImgView with frame before set the image.
It return nil because the myImgView not initalize
myImgView = UIImageView(frame:CGRectMake(10, 50, 100, 300));
It works for me. Hope it will helps you.Thanks alot

I programmatically created rectangles, and added buttons on storyboard and the buttons are getting covered by the rectangles. How do I fix that?

I programmatically created rectangles, and added buttons on storyboard and the buttons are getting covered by the rectangles. How do I fix that?
import UIKit
class interestViewController: UIViewController {
var squareView: UIView!
var gravity: UIGravityBehavior!
var animator: UIDynamicAnimator!
var collision: UICollisionBehavior!
override func viewDidLoad() {
super.viewDidLoad()
squareView = UIView(frame: CGRectMake(100, 100, 100, 100))
view.addSubview(squareView)
}
As #matt mentioned it subViews and basically Views are ordered in the window. So you have different way to do what you want :
You can insert your rectangle at a given index if you know it :
self.view.insertSubview(squareView atIndex:index)
If you have a reference to your UIButton you can either do :
self.view.insertSubview(square, belowSubview:button)
or after adding your squareView using :
self.view.addSubview(squareView)
Call the following :
self.view.sendSubviewToBack(squareView)
All this methods are referenced in the UIView Class Reference
Views have a layering order. After you add a rectangle, move it to the back of the layering order.
you can use this code .
import UIKit
class interestViewController: UIViewController {
var squareView: UIView!
#IBOutlet weak var btntemp: UIButton!
var gravity: UIGravityBehavior!
var animator: UIDynamicAnimator!
var collision: UICollisionBehavior!
override func viewDidLoad() {
super.viewDidLoad()
squareView = UIView(frame: CGRectMake(100, 100, 100, 100))
view.addSubview(squareView)
// add button on squareView
squareView.addSubview(btntemp)
}

Resources