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.
Related
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()
}
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
}
I am using scroll view which scrolling everything perfectly but i have button at the top of view for dissmissing view i want it to remain there and do not scroll in swift .
Add first UIScrollView and then UIButton to self.view.
Add button outside scrollview content like overlay.
Try this solution:
class TmpVC: UIViewController, UIScrollViewDelegate {
#IBOutlet var scoll : UIScrollView!
#IBOutlet var btn : UIButton!
var btnPoint : CGPoint!
override func viewDidLoad() {
super.viewDidLoad()
scoll.contentSize = CGSize(width: 320, height: 600)
scoll.delegate = self
btnPoint = btn.frame.origin
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
btn.frame.origin = CGPoint(x: btnPoint.x, y: btnPoint.y + scrollView.contentOffset.y)
}
deinit {
print("deinit Tmpvc")
}
}
I am using Xcode 8. In my code, I have certain items hidden, but when I launch the simulator to test, those same items show up. Is there something that I am missing?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var logoImg: UIImageView!
#IBOutlet weak var howManyTapsTxt: UITextField!
#IBOutlet weak var playBtn: UIButton!
#IBOutlet weak var tapBtn: UIButton!
#IBOutlet weak var tapsLbl: UILabel!
#IBAction func onPlayBtnPressed (sender: UIButton!) {
logoImg.isHidden = true
playBtn.isHidden = true
howManyTapsTxt.isHidden = true
tapBtn.isHidden = false
tapsLbl.isHidden = false
}
}
My code is above. The logo, howManyTapsTxt, and playBtn should be the only items shown when the simulator is launched. And when the playBtn is pressed, the tapsLbl and tapBtn should be the only items shown. But that is not the case. Any help/guidance is greatly appreciated. Thanks.
Your posted code makes no attempt to set the initial state of any of your views. The typical solution is to set the state in the viewDidLoad method.
override func viewDidLoad() {
super.viewDidLoad()
// Set the initial state of your views here
tapBtn.isHidden = true
tapsLbl.isHidden = true
}
The other option is to mark these views as hidden in Interface Builder.
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