No UIButton Edge option in Xcode 8 - uibutton

I'm using Xcode 8. I'm trying to add a UIButton with Image on left side and Title right side. I would like to maintain distance between Image and Title of the UIButton in Storyboard. I can't see Edge option here. Any help here is much appreciated.

Using Storyboard
Environment : XCode 8
Note : In Xcode 8 you will find the Edge option in Size inspector which named as Content Insets, Title Insets and Image Insets.
Environment : XCode 7
Using Code.
Swift 3
myButton.titleEdgeInsets = UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat);
myButton.imageEdgeInsets = UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat);
Objective C
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)];

Xcode 8 StoryBoard:
First set the title and image to the button, and go to the size inspector and set title insets and image insets, it will align the content to desired location. In your case set title inset left and image insets right.
Programatically:
button.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
button.imageEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
Output:
Xcode 7 StoryBoard:
Xcode 7 Edge option has been moved from Attribute inspector to Size inspector section in Xcode 8, and named as Content insets, Title insets and Image insets.

Related

Make application origin start from top left

I'm building a cross platform application, and on most platforms, when you have a rectangle for like say a window frame position, it normally starts from the top left.
Like for say:
Rectangle rect = new Rectangle(0, 0, 100, 100); // Starts at top left corner
but for Macos it starts on the bottom left.
Is there any way to change this?
Also, I'm looking for this to be applied to NSWindow.
There's a method in NSWindow that allow you to position the top-left corner of the window’s frame rectangle at a given point in screen coordinates:
func setFrameTopLeftPoint(_ point: NSPoint)
Or, You could just do an extra step for macOS to get the flipped y:
flippedY = screenHeight - y - windowHeight

What is the best way to disable horizontal scroll of UIScrollView?

In UIScrollView, I have content height and width greater than scrollview's size. So basically it can scroll horizontally and vertically. What I want is to scroll vertically only and I manage horizontally scroll using an UIButton by using below code.
[scrlViewQuestion setContentOffset:CGPointMake(questionWidth, 0) animated:YES];
Preventing horizontal scrollview, one can just set scrollview content width lesser than scrollview size but in my case scrollview content width is greater than its size? So what is the best way to solve this?
Content UIView width should be equal to the width of UIScrollView's superview for instance, not UIScrollView itself.
SWIFT 5
first, you should create a delegation class or extend the view control with UIScrollViewDelegate, and after, you should check the content offset with scrollViewDidScroll(_:) func and disable it with that:
here is a sample code:
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x != 0 {
scrollView.contentOffset.x = 0
}
}
}
Using this view hierarchy and these constraints
Step by Step explanation of the image
Place your ScrollView inside of a view(OuterView).
Place the view with your content(InnerView) inside the ScrollView
Align the ScrollViews 4 sides to the OuterViews 4 sides
ScrollView.leading = OuterView.leading
ScrollView.trailing = OuterView.trailing
ScrollView.top = OuterView.top
ScrollView.bottom = OuterView.bottom
Align the InnerViews 4 sides to the ScrollViews 4 sides
InnerView.leading = ScrollView.leading
InnerView.trailing = ScrollView.trailing
InnerView.top = ScrollView.top
InnerView.bottom = ScrollView.bottom
Set the InnerViews width and height equal to the OuterViews width, NOT the ScrollViews width.
InnerView.width = OuterView.width
Source
Set the contentSize of your scrollview to the width of your screen and the actual height of your scrollable content. Set the contentInset to zero for the top, bottom, and right. For the content inset on the left always choose the negated x coordinate of the position you currently want to display.
E.g. if your screen is 320 points wide, and you want to display content with horizontal scrolling fixed at x, but vertical scrolling allowed:
CGFloat screenWidth = 320.0;
CGSize actualContentSize = CGSizeMake(3000, 3000);
[scrlViewQuestion setContentSize:CGSizeMake(screenWidth, actualContentSize.height)];
[scrlViewQuestion setContentInset:UIEdgeInsetsMake(0, -x, 0, 0)];
swift 4. set the delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x != 0 {
scrollView.contentOffset.x = 0
}
}
You can do this in storyboard.
Disable the Show Horizontal Indicator tab from the Scroll View's Identity Inspector.It will disable your horizontal scrolling.

is it possible to autolayout the width of the UISlider

I am using PureLayout to put a UISlider between 2 buttons as below:
[self.slider autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.leftButton withOffset:10.0f];
[self.slider autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:self.rightButton withOffset:10.0f];
[self.slider autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
Is it possible that the UISlider automatically sets its width between the leftButton and the rightButton ?
To autolayout the width of UISlider you should provide the width of left and right button. Also, you have to pin them to the superview edges. Then you're able to pin LEFT edge of slider to RIGHT edge of leftButton, and RIGHT edge of slider to LEFT edge of rightButton.
Example code :
cell.contentView.addSubview(leftButton)
cell.contentView.addSubview(slider)
cell.contentView.addSubview(rightButton)
rightButton.autoPinEdgeToSuperviewEdge(.Top)
rightButton.autoPinEdgeToSuperviewEdge(.Right)
rightButton.autoSetDimension(.Width, toSize: 100)
rightButton.autoSetDimension(.Height, toSize: 50)
leftButton.autoPinEdgeToSuperviewEdge(.Top)
leftButton.autoPinEdgeToSuperviewEdge(.Left)
leftButton.autoSetDimension(.Width, toSize: 100)
leftButton.autoSetDimension(.Height, toSize: 50)
slider.autoPinEdge(.Leading, toEdge: .Trailing, ofView: leftButton, withOffset : 5)
slider.autoPinEdge(.Trailing, toEdge: .Leading, ofView: rightButton, withOffset : -5)
slider.autoPinEdgeToSuperviewEdge(.Top)

Adding space above the first tableview section

How do I add space above the first tableview section? You can see that the first section: "A", doesn't have the right above it. Here's an image:
https://docs.google.com/document/d/1QRuL_aoSQNbg-SLlrcAzcQGT1L7gBCTgg-ji-XTHXjY/edit?usp=sharing
Tried changing UITableview content offset in viewDidLoad, but that didn't work. Suggestions?
As UITableView is a subclass of UIScrollView better use contentInset property of your table view as described in "Creating and Configuring Scroll Views" of Apple developer's library.
Simply put this code in a place where you initialize or configure your table view:
CGFloat topInset = 10; //change this value as needed
tableView.contentInset = UIEdgeInsets(top:topInset, left: 0, bottom: 0, right: 0)
UPDATE for Swift 4
let topInset: CGFloat = 10
myTableView.contentInset.top = topInset

iOS 7 Navigation Bar and Scroll View are different in storyboard and simulator

I have a navigation controller with navigation bar, not translucent. I added a scroll view to the root view. But when I run the app, it show different from what I saw in StoryBoard. Everything shifted down.
This is what I saw in StoryBoard:
This is in simulator:
Your storyboard should like this
In ios 7 scroll view must be covered to entire screen
You need to put image on top edge
You will output as below
make a full screen UIScrollView, add a full screen Content View to UIScrollView
add a transparent view to top of this Content View: top:0, left: 0, right: 0, equal width with scroll, height: 64(height of status bar & navigation bar)
hook up Transparent View's height constraint (which is 64) to your ViewController Class as a IBOutlet:
design your view as you wish; I will add a button below the navigation bar: top space to transparent view: 8, left: 8, width: 30, height: 30
add code below to your ViewController Class; if the iOS version is iOS7, set Transparent View's Height Constraint to 0(zero), if it is iOS8, do nothing:
- (void) updateViewConstraints {
[super updateViewConstraints];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
_transparentTopViewYConstraint.constant = 0;
}
}
as a result, all your view's top space is relative to Transparent View, if system version is iOS7, your Transparent View's height will be 0(zero) and your views are move to top, top space will be just 8 for my example, so your views place just below the navigation bar. if system version is iOS8, your Transparent View's height will be 64 and your view's top space will be 8 + 64, so your views place just below the navigation bar again.
please check the following:
is auto layout on? then turn it off
Are you using a simulator of different size? (story board is for 4 inch, while simulator is 3.5 inch) if that so, many things are to set up like turning off autoSizing right and bottom constraints
Hope this helps