I am currently trying some iOS programming. I decided to go with Swift.
Everything is going fine, untill I try to use a UIWebView it won't change it's size to the screen size. I started app development on Android, but afaik there is nothing like relative sizes in iOS right?
Currently this is what my code looks like:
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var mainWebView: UIWebView!
var urlPath = "http://www.google.com"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadWebView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadWebView()
{
var newRect = mainWebView.bounds
newRect.size.height = self.view.frame.size.height
newRect.size.width = self.view.frame.size.width
mainWebView.frame.size = newRect.size
let requestURL = NSURL(string:urlPath)
let request = NSURLRequest(URL: requestURL!)
mainWebView.loadRequest(request)
}
}
I tried different approaches with the above (giving de frame size to the mainWebView, but none work. Maybe it has something to do with the moment I give the commands to change size? Should I change the size earlier in the loading proces?
I also tried this:
mainWebView.sizeThatFits(self.view.frame.size)
But that didn't do anything either.
I would definitely appreciate all help!
Thanks in advance
Friso
PS.
In the example I am using the values from self.frame.size, but also when using for instance 100 x 100, it just won't change the size...
You need to set the whole frame at once. Setting the size of a UIView's frame directly does not work.
var newRect = mainWebView.frame
newRect.size.width = self.view.frame.size.width
newRect.size.height = self.view.frame.size.height
mainWebView.frame = newRect
Related
I wanted to use BCMeshTransformView library into my swift project. I've created an empty swift project and added the library via cocoa pods.
Here's my ViewController class:
import UIKit
import BCMeshTransformView
class ViewController: UIViewController {
var transformView:BCMeshTransformView!
var imageView:UIImageView!
var transform:BCMutableMeshTransform!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
transformView = BCMeshTransformView(frame: self.view.bounds)
transformView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView = UIImageView(image: UIImage(named: "picture.jpg")!)
imageView.center = CGPoint(x: transformView.contentView.bounds.midX, y: transformView.contentView.bounds.midY)
transformView.contentView.addSubview(imageView)
transformView.diffuseLightFactor = 0.0
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
self.view.addSubview(transformView)
}
}
When I'm running the app nothing is showing. It's entirely white.
Removing this code:
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
doesn't change anything.
But when in xcode I'm switching to "Show UI hierarchy" I can see an image:
Here's a whole sample project:
http://www116.zippyshare.com/v/IUTXbKJg/file.html
Why I cannot see anything? I tried making an example as simple as possible.
Looks like that was a problem with that library codun't find shader files. I had to copy BCMeshShader.fsh and BCMeshShader.vsh to my project.
I am relatively new to swift and have been used to a JavaScript environment so correct me if it is not possible, but is there a way to dynamically add rows of text inputs and thus data in the form of a nested dictionary, from the view via a button?
I am looking to create an object with no set constraints on the limitations of data.
Essentially the object would be structured as:
"Object1": ["row1":[desc: "desc1", option: "option1"]]
and when a user adds a row through a UIButton:
"Object1": ["row1":[desc: "desc1", option: "option1"], "row2": [desc: "desc2", option: "option2"]]
and so on....
Image example of what I am looking to achieve
Thank you for any input provided.
With the feedback of Will M I did overthink this again and came up with another solution:
class ViewController: UIViewController {
let xPos : CGFloat = 0
var yPos : CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func press(sender: AnyObject) {
yPos += 22
let tf = UITextField()
tf.frame = CGRectMake(xPos, yPos, 100, 20)
tf.backgroundColor = UIColor.blackColor()
self.view.addSubview(tf)
}
}
Here is a screenshot of what the result looks like I hope this helps you!
http://imgur.com/m59xDfN
The black things are textfields
I'm experiencing an unusual issue where the keyboard correctly responds to input focus actions in the simulator (hardward keyboard disabled), but when I build and test on an actual device, the keyboard doesn't appear.
The app is a simple SFSafariViewController. Do I need to specify keyboard settings in Info.plist or something similar?
----- Update -----
Adding source code:
import UIKit
import SafariServices
class ViewController: UIViewController
{
private var urlString:String = "https://example.com"
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// This will remove the status (battery, time, etc) bar
UIApplication.sharedApplication().statusBarHidden = true
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
// Kind of a hack, in that we really aren't removing the navbar
// Rather we are adjusting the starting point of the vpc object so it appears as the navbar is hidden
self.presentViewController(svc, animated: true) {
var frame = svc.view.frame
let OffsetY: CGFloat = 42
frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
frame.size = CGSize(width: frame.width, height: frame.height + OffsetY)
svc.view.frame = frame
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Ugh. Being new to ios development I was 100% certain my code was borked, which was causing the keyboard not to appear. So this morning I created a test project and added each line of code from my project to the test project to see which was causing the perceived breakage.
After all the code was added back, I re-compiled the test project again, and it worked. Same exact code as the normal project, wtf? So then I realized there must be some caching anomaly that is causing the issue, and sure enough I found this post on how to clear Xcode cache. After clearing the cache I re-loaded my project, re-compiled, and sure enough, my project worked as expected.
So in short, when in doubt, clear that cache.
I'm capturing a full page screenshot of a UIWebView and passing the image through a segue:
// WebViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "captureSegue" {
var captureViewcontroller:CaptureViewController = segue.destinationViewController as! CaptureViewController
captureViewcontroller.tempCaptureImage = image
}
}
The UIImage is then being saved to Documents/Images in the application directory just fine. If I open it from the file system it shows just fine in Preview. However, if the screenshot is really large, the images won't show in a UIImageView. The UIImageView renders blank. Adding a breakpoint shows that the image data is there, just not rendering.
// CaptureViewController.swift
var tempCaptureImage: UIImage?
#IBOutlet weak var imageScrollView: UIScrollView!
#IBOutlet weak var captureImageView: UIImageView!
#IBOutlet weak var titleField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let image = tempCaptureImage {
captureImageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
captureImageView.image = image
imageScrollView.contentSize = image.size
let scrollViewFrame = imageScrollView.bounds
let scaleWidth = scrollViewFrame.size.width / imageScrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / imageScrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight);
imageScrollView.minimumZoomScale = minScale;
imageScrollView.maximumZoomScale = 1.0
imageScrollView.zoomScale = minScale;
}
}
Could this be because the image is large? If the pages that I capture are smaller, then the images show just fine. Are there any recommended techniques for viewing large images on iDevices?
EDIT:
Here is the output of the Variables View with a breakpoint. Maybe you see something I'm missing.
For people have similar problem, maybe tried to test in real device. For me, image with size 1000 x 7000 isn't displayed in Simulator nor Interface Builder, but can be displayed in an iPhone 6s plus.
Also, Interface Builder will complain if image size is larger than 10000 x 10000.
You do not give any info on how large the image actually is, so it's impossible to say what limit you might be hitting. Remember, though, that a triple-scale image multiplies the underlying bitmap memory size by 9 in comparison to a single-scale image. That's an order of magnitude! I can well believe that you would quickly exceed the memory capacity of the app and even crash if you tried to access an image that's too large. So step one would surely be to load the image as a single-scale image; see the ImageIO framework to learn how to do that. See also the docs on CATiledLayer if you want to know how to display a very large image.
I just want to modify the height of a UITextView.
The log resulting from the code below, says that in fact, the
height changed from 30 to 400:
println(txtresponses.frame.height) // returns 30
var newFrame:CGRect=txtresponses.frame
newFrame.size.height=400
txtresponses.frame=newFrame
println(txtresponses.frame.height) // returns 400
However, visually, the UITextView "txtresponses" remains with the same size.
I am new to Swift and Xcode, so all my tricks are already exhausted here, and I dont know if it is an iOS version issue, or some typical Xcode whim.
What is the correct way to modify a UITextView ´s height?
Make sure to call txtresponses.frame=newFrame in the main thread.
dispatch_async(dispatch_get_main_queue()) {
txtresponses.frame=newFrame
}
All UI updates must be done from the main thread.
Its not work because I think you are using Autolayout with constraint. Please check below url which may help you - Change height constraint programmatically
Might be issue Autolayout. u just remove the autolayout and check it will work. Check below code i hope it will help you.
Example :
import UIKit
class ViewController: UIViewController {
#IBOutlet var textView: UITextView!
#IBOutlet var butt: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
textView.backgroundColor = UIColor.lightGrayColor()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonAction(sender: UIButton) {
var newFrame:CGRect=textView.frame
newFrame.size.height=400
textView.frame=newFrame
}
}
Screen 1:
Screen 2: