FastNavigator
How can i fix this issue in making a scroll view programatically?
class FastNavigator: UIView {
var scrollView : UIScrollView!
var scrollSubviews : [UIView]!
var size :CGFloat = 0
func configure(subviews:[UIView]){
scrollSubviews = subviews
scrollView = UIScrollView(frame: self.frame)
self.addSubview(scrollView)
for view in scrollSubviews {
scrollView.addSubview(view)
view.frame = CGRect(x: 0 + size , y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
size += view.frame.size.width
print(size)
}
scrollView.contentSize = CGSize(width: size, height: scrollView.frame.height)
}
}
ViewController
class ViewController: UIViewController {
let fast = FastNavigator()
let myImages = ["mahmoud.jpg","ziad.jpg","farah.jpg","zinji.jpg"]
override func viewDidLoad() {
super.viewDidLoad()
var subViews : [UIImageView] = []
for imageName in myImages {
let myUIImage = UIImage(contentsOfFile: Bundle.main.path(forResource: imageName, ofType: "")!)
let imageView = UIImageView(image: myUIImage)
subViews.append(imageView)
}
fast.frame = self.view.frame
fast.configure(subviews: subViews)
self.view.addSubview(fast)
}
}
And am always getting this
libc++abi.dylib: terminating with uncaught exception of type NSException
Go to Breakpoint Navigator, click add ("+" button), "Exception Breakpoint", RMB, "Edit breakpoint", add action with "po $arg1". Now you got printed error of NSException. Please post your error here.
Related
I'm subclassing the login screen of Firebaseui with:
import UIKit
import FirebaseUI
class LoginViewControllerCustom: FUIAuthPickerViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
let arr = Bundle.main.loadNibNamed("LoginText", owner: nil)!
let v = arr[0] as! UIView
self.view.addSubview(v)
}
}
My implementation works as I see the xib LoginText loaded on login screen.
But the background color is royally ignored.
How to enforce a bg color on the login screen from that subclass?
Edit: if I apply the answer below with view.insertSubview(imageViewBackground, at: 0)
Here is what I get:
As you can see the image gets inserted under the view that holds the login button. If I set "at: 1" it completely cover the buttons and they can't be used.
I resolved the problem in an unexpected way.
On the delegate method that would load this controller, I changed:
func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
return LoginViewControllerCustom(authUI: authUI)
}
to
func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
return LoginViewControllerCustom(nibName: nil, bundle: Bundle.main, authUI: authUI)
}
The addition of Bundle.main solved the issue, and replaced the original controller by mine, which was several levels deeper until that.
Not sure exactly why, but this did solve the issue.
you can try to put "fake" image background:
override func viewDidLoad() {
super.viewDidLoad()
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width:
width, height: height))
imageViewBackground.backgroundColor = .red
view.insertSubview(imageViewBackground, at: 0)
let arr = Bundle.main.loadNibNamed("LoginText", owner: nil)!
let v = arr[0] as! UIView
self.view.addSubview(v)
}
Edit: try this it's not elegant but it solves the problem.
override func viewDidLoad() {
let scrollView = view.subviews[0]
scrollView.backgroundColor = .clear
let contentView = scrollView.subviews[0]
contentView.backgroundColor = .red
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let backgroundImage = UIImageView(frame: CGRect(x: 0, y: -1, width: width, height: height))
view.backgroundColor = .red
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
view.insertSubview(backgroundImage, at: 0)
}
I have a custom text field and i want to place image on its left side, but when ever i am running the app, the size of image is not adjusting , i.e its full scale and not taking the width and height being provided. The code and pictures are attached
ViewController class:(In which text field is present)
import UIKit
class Signup2ViewController: UIViewController {
#IBOutlet weak var Email: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
Email.leftViewMode = .always
let imageview = UIImageView()
imageview.frame = CGRect(x: 0, y: 0, width: 5.0, height: 5.0)
view.addSubview(imageview)
let icon = UIImage(named: "c.png")
imageview.image = icon
Email.leftView = imageview
// Do any additional setup after loading the view.
}
}
To set the frame for your imageView you need to subclass UITextField and override leftViewRect(forBounds:). The code below will result in a 20x20 view offset 10 points from the left and centered vertically.
class AwesomeTextField: UITextField {
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
let leftViewHeight: CGFloat = 20
let y = bounds.size.height / 2 - leftViewHeight / 2
return .init(x: 10, y: y, width: leftViewHeight, height: leftViewHeight)
}
}
To add an imageView to the textField you would do this:
class ViewController: UIViewController {
#IBOutlet weak var textField: AwesomeTextField!
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
imageView.image = UIImage(named: "c")
textField.leftView = imageView
textField.leftViewMode = .always
}
}
Make sure you set the appropriate class name for the textField in the identity inspector of your storyboard.
I have a UIScrollView, and am using a UIPageControl to present images. The moment the first image appears, the program crashes and indicates a nil value was found.
EDIT: I'm no longer downloading images for the Scroll View, yet I'm still getting the error. The image appears and then the program crashes. Here is the stripped down code. Totally at a loss at this point. FYI, the reason I declare the variable i is I intend to loop and add additional images:
import UIKit
class HistoryViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
var frame = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 0, height: 0))
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.isPagingEnabled = true
let i = 0
frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
frame.size = self.scrollView.frame.size
let subView = UIImageView(frame: frame)
subView.image = UIImage(named: "maudite")
subView.contentMode = .scaleAspectFit
self.scrollView.addSubview(subView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * CGFloat(1), height: self.scrollView.frame.size.height)
}
}
.....................
I have two theories why this is happening. The first is that The images for the subView that I add to the ScrollView haven't loaded yet. I'm using AlamoFireImage, which downloads images asynchronously.
This is the code that configures the Scroll View:
func configureScrollView() {
self.myScrollView.isPagingEnabled = true
for index in 0..<imageURLS.count {
frame.origin.x = self.myScrollView.frame.size.width * CGFloat(index)
frame.size = self.myScrollView.frame.size
let subView = UIImageView(frame: frame)
if let url = URL(string: imageURLS[index]) {
subView.af_setImage(withURL: (url))
subView.contentMode = .scaleAspectFit
self.myScrollView.addSubview(subView)
}
}
self.myScrollView.contentSize = CGSize(width: self.myScrollView.frame.size.width * CGFloat(imageURLS.count), height: self.myScrollView.frame.size.height)
pageControl.numberOfPages = imageURLS.count
}
The other thing that might be the cause of the issue is that I first have to download the image URL's. I do this and store them in an array. When that is complete, I call the configureScrollView(). I do the call on the Main Thread. Does the function then run on the Main Thread?
for dict in responseArray {
if let url = dict["fileName"] as? String {
let fullURL = "http://www.smarttapp.com" + url
print(fullURL)
self.imageURLS.append(fullURL)
}
}
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
self.activityIndicator.isHidden = true
self.networkingState = .finishedSearching
self.configureScrollView()
}
}
I am making an app that the user can upload 1 to 3 photos.
I want the user to be able to see them it as a slider in with Page Control,
I started to code it but for some reason the images do not added to the scrollView (I see nothing when running the app).
This is my code so far:
class dogProfileViewController: UIViewController {
var imagesArray = [UIImageView()]
#IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let image1: UIImageView = UIImageView(image: UIImage(named: "image1.png"))
let image2: UIImageView = UIImageView(image: UIImage(named: "image2.png"))
imagesArray.append(image1)
imagesArray.append(image2)
var countIndex = CGFloat(0)
for image in imagesArray {
image.frame = CGRectMake(countIndex*view.frame.width, 0, view.frame.width, 100)
scrollView.addSubview(image)
countIndex = countIndex + 1
}
// Do any additional setup after loading the view.
}
}
Add the following line after the for loop ends
scrollView.contentSize = CGSizeMake(countIndex*view.frame.width,0)
I think this will work.
#IBOutlet var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 175)
imageArray = [#imageLiteral(resourceName: "cake"),#imageLiteral(resourceName: "food")]
for i in 0..<imageArray.count{
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleToFill
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
}
Well, according to what you provided, you haven't added the scrollView to the view. So, first step, make sure the scrollView is added to the view. self.view.addSubiew(scrollView). Change the scrollView background to make sure it is there.
Also, I don't see that you have initialized the scrollView frame. The scrollView has no frame, so it won't show anywhere. Make sure that you initialize the scrollView.
I am trying to use this plugin as refresh action : https://github.com/entotsu/PullToBounce
One, issue is I can't understand his explanation.
Explanation given on the github
tableView.frame = yourFrame --> tableView is equal to scrollView.frame in my situation
yourFrame --> I have no idea what it is. The main frame ? Another Frame I have to create ?
bodyView.addSubview(tableViewWrapper) --> bodyView ? Main Frame here ? or Another frame ?
Here is my code for the scrollView for now. Any help on how to implement this plugin using a scrollView made via the storyboard.
class ProfileViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
func makeMock() {
let headerView = UIView()
headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 64)
headerView.backgroundColor = UIColor.lightBlue
self.view.addSubview(headerView)
let headerLine = UIView()
headerLine.frame = CGRect(x: 0, y: 0, width: 120, height: 8)
headerLine.layer.cornerRadius = headerLine.frame.height/2
headerLine.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.8)
headerLine.center = CGPoint(x: headerView.frame.center.x, y: 20 + 44/2)
headerView.addSubview(headerLine)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blue
let bodyView = UIView()
bodyView.frame = scrollView.frame
bodyView.frame.y += 20 + 44
self.view.addSubview(bodyView)
let tableViewWrapper = PullToBounceWrapper(scrollView: scrollView)
bodyView.addSubview(tableViewWrapper)
tableViewWrapper.didPullToRefresh = {
NSTimer.schedule(delay: 2) { timer in
tableViewWrapper.stopLoadingAnimation()
}
}
makeMock()
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
One thing, I notice is that there is a View on top of my scrollView that disable me to view it and scroll it. Help here needed please.
Regards,
Hary
Take a look at the Example of this library.
yourFrame is nothing but your tableview class. For example if your tableView Class is named SampleTableView, then it goes like
let tableView = SampleTableView(frame: self.view.frame, style: UITableViewStyle.Plain).
You have to use another class to set up your tableView.