Swift 3 Can't touch UIButton on the bottom of UIScroll - ios

I have a problem with Xcode 8 and Swift 3 UIButton inside UI ScrollView which the UIButton can't touch/tap. The structure of storyboard like this.
View
-Scroll View
--Content View
---Content Label (dynamic long content get from API)
---Agree Button
And this lines of codes for making scroll working with the dynamic label.
override func viewDidLayoutSubviews() {
let maxLabelWidth: CGFloat = self.contentView.frame.size.width
let neededSize = self.contentLabel.sizeThatFits(CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude))
self.contentView.frame.size.height = neededSize.height+self.agreeButton.frame.size.height+300
self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.contentView.frame.size.height)
}
Everything is setup as default in the storyboard. Is there another configuration to solve this problem?

It is highly likely that your UIButton is not within the bounds of its superview. That is usually the problem when I see this happen. Try setting the background color of the UIButton's superview to red or something and see if the button is within the red area.

Related

Replace view inside UIStackView

I have a UIStackView with 5 buttons inside. One of those buttons (#4) needs to be replaced by a UIPickerView once it is clicked and switched back to the UIButton once one of the other four's is selected.
The UIStackView is horizontal, alignment is fill and distribution is Fill Proportionally.
The stack was done using storyboard, and I am trying to replace the view with:
override func viewDidLayoutSubviews(){
self.pickerView.frame.origin = self.view.viewWithTag(40)!.frame.origin
self.pickerView.frame.size = CGSize(width: 30, height: 30)
}
But my UIStackView will not rearrange after the button click, and button #5 is staying on top of the UIPickerView.
Any ideas?
Ok, I found the code. I had to replace addView for addArrangedSubview.
Final code (on button press):
let stack = (self.view.viewWithTag(20) as! UIStackView)
stack.addArrangedSubview(self.pickerView)

Scrollview not displaying items

I am trying to display a UIView on a UIScrollView, and the UIView's will display horizontally lined-up. However, only 1 Item appears in the ScrollView when I scroll it horizontally.
override func viewDidLoad() {
self.scollV.contentSize = CGSize(width: self.view.frame.width * 10, height: 200)
for i in 0..<10 {
self.myview.frame.size.width = self.view.bounds.size.width
self.myview.frame.origin.x = CGFloat(i) * self.view.bounds.size.width
self.label.text = "\(i)"
self.myview.addSubview(self.label)
self.scollV.addSubview(self.myview)
}
}
I can see that you just started programming in swift for ios, and there are so many basic errors in your code.
First things first.
You have only one UIView instance created, and you are adding it multiple times to the scroll view, that's why you have only 1 item on the scroll view. Create items (UIView instances) inside the cycle, and add them to the scroll view. (Really hope you know how to do it)
No need to add scroll view in the cycle, this is wrong, you should add it only once.
Read ios programming basics!

How to add a decorator at left of a single paragraph in a UITextView?

I have a custom UITextView, and I have several styles for it.
One of them includes adding a vertical line all to the left besides the actual text for an specific paragraph. This line should have the same size as the paragraph and scroll with it.
How can this be achieved using UITextView?
Basically you can take another approach for your problem by add a UIView or CALayer with a filled background to the left of your UITextView. Just set its width value to 2-3 to equate it as the 'left border'.
extension UITextView {
func setLeftBorder(color: UIColor, width:CGFloat = 2.0) {
let borderFrame = CGRect(x: 0, y: 0, width: width, height: self.frame.width)
let borderView = UIView(frame: borderFrame)
borderView.backgroundColor = color
self.addSubview(borderView)
}
}
But the problem will not solved when your UITextView starts scrolling. So I think you should embed them together inside another UIView. Here is my final solution by using AutoLayout:
My Solution - Click to see the image

How to change UIToolbar height? [duplicate]

This question already has answers here:
Is there a way to change the height of a UIToolbar?
(10 answers)
Closed 6 years ago.
I want to change the height of the UIToolbar in Navigation Controller, but I am not able to do so in swift with Xcode 7.3 and iOS 9. I have tried to set frame with CGRectMake at viewDidLoad, but it didn't work. Also tried to init a custom UIToolbar(), but the height remain the same.
Edit:
As per one of the answers, I have tried selected toolbar, press control and select the toolbar again, but what I got is shown in below screenshot instead:
There are 2 options depending on whether you are using Storyboards or not.
Option 1: Using Storyboard
1) Go to your Storyboard and from your selected Toolbar press and hold Ctrl and click on your Toolbar again as if you were assigning an IBAction. Then you will get the following:
(Sorry for the quality of the first image; had to take a snap with my phone because I couldn't make a screenshot while holding ctrl)
2) Then press on height to get the constraint and change the value:
Option 2: Using Swift for e.g. 55 px height
yourToolBar.frame = CGRect(x: 0, y: view.frame.size.height - 55, width: view.frame.size.width, height: 55)
You can use it with ;
Change 45 for min or max heights.
override func layoutSubviews() {
super.layoutSubviews()
var frame = self.bounds
frame.size.height = 45
self.frame = frame
}
override func sizeThatFits(size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
size.height = 45
return size
}
Use it with like ;
let navigationController = UINavigationController(navigationBarClass: nil, toolbarClass: Toolbar.self)
Thanks
If you use auto layout, you could set a Height Constraint in the Storyboard.
Or you can do it programmatically like this:
myToolbar.frame = CGRectMake(0, 50, 320, 35)

How to set full width image inside UIScrollView in swift

I need to display large content in my UIViewController added in Storyboard so I added UIScrollView with constraints top, right, bottom, left:0 ( to make it full screen ).
In top of UIScrollView I need a square image with its width as device screen size, so I added UIImageView with constraints : aspect ratio->1:1, top:0, centre align in container, height : 100.
And below It there is a UILabel where I want to show large text. I am using Auto Layout.
Is there an option to do this in Storyboard ?
In swift I tried below
Connected Image in controller file as :
#IBOutlet weak var eventThumb: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// set image path from url ,it works
eventThumb.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.event!.image)!)!)
let screenSize: CGRect = UIScreen.mainScreen().bounds
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.width)
}
I tried related answers given here ,here and here but they not seem to working in my case.
I am new in swift and ios, am I doing something wrong in structure ?
Edit :
Image added
Call it in:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// your layout code goes here, AFTER the call to super
}
You forgot to call super before your code.
You had tried to make the frame with following screen size
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.width)
Please crosscheck it with
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.height)
Simply Try this Steps:
First Remove all red lines here you can see in Image
Now keep that line as it is, where you are setting a frame of Imageview
Note:
I already tried this solution in swift & worked for me. I hope it will work for you.
Edited:
As you mentioned you are using Auto layout, So no need to disable it. Just do 1 thing
Put the line which you are setting up the frame of image view in this method:
-(void)viewDidLayoutSubviews

Resources