How to change UIToolbar height? [duplicate] - ios

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)

Related

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

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.

How to Put UIView subview In Places

For Example lets say I have a UIView I would like to display
view.addSubview(line)
Now I want t to be able to display that UIView in the top right corner. Let say inside the right side of the navigation bar. How would I do this? Or even in a specific place.
You will need to calculate the frame or need to give constraints to that view.
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
From above you can get screenwidth and you can set your y to 0. Let say width height of your view is 20. So, frame would be like below.
yourView.frame = CGRect(x:screenwidth - 20 , y: 20, width:20 , height: 20)
You can change the frame as per your requirements.
For navigation bar there is a better way to do it by adding right bar item. Look below code how to do it.
self.navigationItem.rightBarButtonItem = rightButtonItem
You can use
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: lineMat)
to set up a view on navigationBar right side.
Also you can add the lineMat view on UIApplication.shared.keyWindow
Just use UIApplication.shared.keyWindow?.addSubview(lineMat) and set up to lineMat view's frame.
Add a frame for your subview, as :
Objective-C:
line.frame = CGRectMake(0, 0, 50, 50);
Swift 3:
CGRect(x:0,y:0,width: 50,height: 50)
This will add your line subview on top left corner with height 50 and width 50.

Swift - setting UIView's size based on size screen

I went through many threads here and tried two most recommended solutions.
Inside ViewDidLoad() method:
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height * 0.254)
or
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.254)
Also, in my storyboard I set low priority of the view's height constraint
(If I don't set height in storyboard, xcode would complain about ambiguous layout)
But none of these lines of code above does anything to darkBackgroundWithButtons, it remains the same height for each device size
This probably is the problem:
In interface builder you set constrains to your button, and therefore it doesn't change its height when you try to update the frame. Do this instead:
First connect your constrain from interface builder to your viewcontroller, just how you would normally do it with a button.
Then use this code to change the constrain:
var index = self.darkBackgroundWihButtons.constraints.indexOf("your constrain name")!
self.darkBackgroundWithButtons.constraints[index].constant = 0.2 // or whatever number you want

Swift + UIView: How to change y coordinates?

I have a UIView called descriptionView and I want to hide it initially when the screen first loads by offsetting the y coordinate to be the screen size + the height of descriptionView itself:
However, in my controller, none of my frame changes do anything:
override func viewDidLoad() {
super.viewDidLoad()
...
// descriptionView.frame.origin.y = self.view.frame.height
// UIView.animateWithDuration(1, animations: {
// self.descriptionView.frame.origin.y = self.view.frame.height
// self.view.layoutIfNeeded()
// })
//
print("xxx")
descriptionView.frame = CGRectMake(0, self.view.frame.height, self.view.frame.width, 66)
// descriptionView.frame = CGRectOffset(descriptionView.frame, 0, descriptionView.frame.height)
}
No matter what I do it seems fixed at that visible position like in my storyboard. Can someone help?
In IB you are using NSAutoLayout, so you either need to manipulate the constraints, or tell the view to translate the mask to constraints.
If you want to set the frame directly then you will want to do this:
descriptionView.translatesAutoresizingMaskIntoConstraints = true
descriptionView.frame = CGRectMake(...)
Otherwise you can create IBOutlets to the height and width constraint from IB and update those:
self.descriptionViewHeight.constant = self.view.frame.width
Additionally, I would recommend doing frame manipulations inside of viewWillAppear: rather than viewDidLoad. viewDidLoad does not strictly guarantee final position.
Instead of editing the frame or descriptionView, edit its height constraint.
First, create an NSLayoutConstraint from this constraint by cmd-dragging the height constraint from the Interface Builder to your class (like you do for any UI object).
Then you can set the constant property of this constraint to 0.
(Yes, constant is declared as a varproperty...)
If you are using constraints, chaging the frame view myView.frame property will not affect on view actual position and size. Instead of this make constraint outlet and change it in your code, it will look like this:
descriptionView.heightConstraint.constant = 100
Also, if you want to hide something, you can use property myView.hidden = true, or myView.alpha = 0;

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