SWIFT - changing height of UITextView, xcode 6 GM - ios

I'm having some trouble changing the height of a simple textview. It doesn't need to size automatically to the content or anything, I just need to change the height when an iad banner loads and when it's hidden.
all I can find on it, is autoresizing UITextFields.
Hopefully someone can help me out with this simple issue.
The textView was just created in storyboard, but here's the code I currently have related to it:
#IBOutlet var textViewOutlet: UITextView!
var textView: String = ""
//after view loads
textViewOutlet.text = textView
here's what I tried adding in the viewDidLoad:
textViewOutlet = UITextView(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 40.0))
Which unfortunately doesn't change the size of the textView at all.
The current height of the textView in the storyboard and the app is: 283
*In another viewController I'm also editing the text in the textView using:
detail.textView = "New text here"
which works fine, don't think that has anything to do with the size of the textview but that's everything I have regarding this particular textView

I ended up having to make the textview through code to make it work.
If anyone else is having trouble with resizing a storyboard textView here's what I added to my code to make it manually.
var textView: UITextView = UITextView(frame: CGRect(x: 5.0, y: 238.0, width: 315.00, height: 283.00))
textView.text = textViewText
textView.editable = false
textView.backgroundColor = UIColor.clearColor()
textView.font = UIFont(name: "Helvetica", size: 15)
self.view.addSubview(textView)
Hopefully that helped someone.
*Works with Swift in xcode 6 GM

Try this:
myTextView = UITextView(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 40.0))
Hope this helps.. :)

Related

How to do UITextView scroll detect and change images

It looks like this :
(I can't show image please I don't have 10 prestiges)
let departmentMessage = UITextView(frame: CGRect(x: 25, y: 1500 / 2, width: UIScreen.main.bounds.size.width - 50, height: (UIScreen.main.bounds.size.height - 50)/4 ))
showBlshText()
departmentMessage.isScrollEnabled = true
departmentMessage.isPagingEnabled = true
departmentMessage.isEditable = false
departmentMessage.isSelectable = true
departmentMessage.dataDetectorTypes = UIDataDetectorTypes.link
departmentMessage.textColor = UIColor.darkGray
departmentMessage.textAlignment = .left
departmentMessage.contentMode = .topLeft
departmentMessage.backgroundColor = UIColor(displayP3Red: 100, green: 100, blue: 100, alpha: 20)
departmentMessage.font = UIFont(name: "Helvetica-Light", size: 20)
self.view.addSubview(departmentMessage)
departmentMessage.removeFromSuperview()
I expect the output of the word to change, but the actual output isn't work.
UITextView is a subclass of UIScrollView. So everything that a scrollView does a textView can do also. In you case, it seems that you want to do some action when the textView is scrolled. You should look the UIScrollViewDelegate documentation which describes how you can monitor a scrollView to see when it is scrolled. There are a few tips to know:
the delegate is not called when you set the content offset programatically.
scrollViewDidEndDecelerating: is not called when scrollViewDidEndDragging:willDecelerate: is called with NO for the willDecelerate value.
So just set your viewController as the delegate, implement the appropriate methods, and change your UI accordingly.
Also, you might have better luck using a UICollectionView or a UITableView. Even if every row is just a label, it is far easier to track what indexPath is at the top of the collectionView then what text is at the top of textView. But without knowing exactly what you are doing it is hard to say.

Xcode scrollview not working

I'm making a game and need users to sign an End User License Agreement (EULA). The EULA is long and requires a scroll view. I want the screen itself to stay still, so the buttons and header are always present:
Here is a screenshot of my viewController storyboard:
here's my code:
#IBOutlet var eulaView: UIView!
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let width = eulaView.frame.width
let height = eulaView.frame.height
self.scrollView.frame = CGRect(x: 0, y: 0, width: width, height: height)
self.label.frame = CGRect(x: 0, y: 0, width: width , height: 2000)
self.label.text = "Lots of stuff"
}
Strangely, if I set self.label.frame's width to 100, it shows up. If I have it at 300, or "width", the label disappears.
No matter what, the scroll view doesn't scroll.
PS: I had it working earlier, but then I tried it on an iPhone X simulator and it was blank.
You need to set self.scrollView.contentSize = CGSize(width: width,height: 2000)
Sometimes When you change device in storyboard at that time screen was blank may be its bug of xcode.
To find the Dynamic Height Of label Depends on String Of label.
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}

Reproducing Music App's NavigationBar Animation

In my project I want to achieve a very similar animation like in the Artist-ViewController of the stock Music App.
When the UITableView scrolls to a specific offset the navigationBar's tintColor, the color of the title and the color of the statusbar changes.
To achieve that I added an UIView (I named it containerView) as a subView of the UINavigationBar and inside that containerView I added a UIBlurEffect. Now when the tableView scrolls I am listening to scrollViewDidScroll to change the earlier named attributes which works really good.
containerView.frame = CGRect(x: 0, y: -(statusBarHeight), width: UIScreen.main.bounds.width, height: frame.height + statusBarHeight)
containerView.clipsToBounds = true
insertSubview(containerView, at: 0)
let blurEffect = UIBlurEffect(style: .extraLight)
overlayView.effect = blurEffect
overlayView.backgroundColor = unOverlayColor
let height = frame.height + statusBarHeight
overlayView.frame = CGRect(x: 0, y: containerView.frame.height, width: containerView.frame.width, height: height)
containerView.addSubview(overlayView)
My only problem is that the containerView is placed above the UINavigationBar's navigationItems and therefore hides them.
So my question is how can I add the containerView behind the navigationItems to the UINavigationBar?
I figured out that it has to be something with the way how navigationBars are handled in iOS 11 because on iOS 10 everything works fine.

Change the height of UISegmented Control using IB?

How can i change the height of UISegmented control. I'm using Swift 3.0 with xcode 8. Height property is disabled by default.
I found this:
https://stackoverflow.com/a/41889155/7652057
#IBDesignable class MySegmentedControl: UISegmentedControl {
#IBInspectable var height: CGFloat = 29 {
didSet {
let centerSave = center
frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: height)
center = centerSave
}
}
}
https://stackoverflow.com/a/37716960/7652057
One of three options from the link,
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
segment.frame = rect
}
Ok, I was trying to do it for a long while and here is the solution.
Firstly, It is possible within IB but for that we need to have a good bunch of autolayout constraints.
I've placed that Segmented control in a UIVIew with all the edges pinned inside it.
Then I gave the desired height to that view and it worked.
Also.. thanks to all of your answer
It's very easy. You can access it programmaticly by using frame's height:
yourSegmentedControllOutlet.frame.size.height = yourHeight

Position of rightView UITextField

Is there a way to adjust the position of a rightView on UITextField? I tried setting the frame on the view (set as rightView) but it didn't change anything.
I'd like to avoid making two views, one as the rightView and one as the rightView's subview where I change the subview's position, if possible.
The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver.
So I suggest you subclass UITextField and override this method, something like this:
#interface CustomTextField: UITextField
#end
#implementation CustomTextField
// override rightViewRectForBounds method:
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
CGRect rightBounds = CGRectMake(bounds.origin.x + 10, 0, 30, 44);
return rightBounds ;
}
#Puneet Sharma's answer was great but that would give the need to create a class that would subclass UITextField, what I did instead was create a UIView that would act as a padding.
This code works without the need to subclass
Here's my code, although it's written in Swift 3
// this is the view I want to see on the rightView
let checkImageView = UIImageView(image: UIImage(named: "check24.png"))
checkImageView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
checkImageView.curveEdges(12)
checkImageView.contentMode = .scaleAspectFit
// declare how much padding I want to have
let padding: CGFloat = 6
// create the view that would act as the padding
let rightView = UIView(frame: CGRect(
x: 0, y: 0, // keep this as 0, 0
width: checkImageView.frame.width + padding, // add the padding
height: checkImageView.frame.height))
rightView.addSubview(checkImageView)
// set the rightView UIView as the textField's rightView
self.textField.rightViewMode = .whileEditing
self.textField.rightView = rightView
What happened here is, that the rightView which is a UIView that has a transparent colored background which then gave the illusion that there is a padding whereas there is not.
Right Padding you can use as
let imageview = UIImageView(image: UIImage(named: "image name"))
imageview.contentMode = .center
let rightPadding: CGFloat = 14 //--- change right padding
imageview.frame = CGRect(x: 0, y: 0, width: imageview.frame.size.width + rightPadding , height:imageview.frame.size.height)
textField.rightViewMode = .always
textFieldd.rightView = imageview

Resources