Trying to make auto layout work with the keyboard extension. Initially i thought i will make the buttons programmatically but then i realized its better to do it with a xib because of my requirement and multiple screen sizes.
Please see the screenshot below on the current configuration i have made.
Button 2:
Button 1 and issue on the app:
All the constraints configuration looks like this:
All i am trying to do here is to make sure the button fills up the screen width. They can expand in width to match screen sizes and orientations. Somehow i feel that its not able to understand the device width. Do i need to specify something for that?
Thanks
Make sure to set the UIView's dimensions on viewDidLoad so that it looks like something like this:
self.mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
I had the same problem and this did the trick for me. Your constraints are just fine.
You do not have a constraint on the distance between the two buttons. Try adding on constraint between the buttons for each button.
Ben Flores answer did the trick for me, too, but I had to put my code in viewDidLayoutSubviews. Otherwise my keyboard would crash and not show up. Swift 3 Version:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let frameSize = self.view.frame.size
self.mainView.frame = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)
}
Related
Context
I have a window with a fullSizeContentView and a transparent titlebar and hidden titlebar.
I don’t even want the titlebar, but I had to enable it to get rounded corners on the NSWindow.
Problem
An NSTextView, when placed near the top edge, doesn’t react to any clicks. It doesn‘t let me select any text, and doesn’t let me click links I added via NSAttributedString.
This issue disappears when I disable the titlebar altogether.
Any Help would be greatly appreciated. Thanks!
Big Context
I’m trying to implement little “in-app notifications” that show peripheral status updates. I considered using NSAlert but I don’t want to prevent the user from interacting with the rest of the interface while the notifications are showing, so I decided to implement it myself.
The notifications are little, non-movable windows without a titlebar. They are basically just grey rectangles with rounded corners and a shadow that draw inside the main application window and contain one or a few lines of text. The first line of text is almost entirely behind the invisible titlebar which is why I’m having issues.
The only thing I need the notifications to do besides display text is link to webpages that contain more info about a notification’s message.
I feel like I might be approaching this wrong. If you have any suggestions or ideas on how to solve the problem, I’m eager to hear them. Thanks!
I finally figured it out!
I nailed it down to the contentInsets of the NSScrollView. (which your are for some reason forced to have around your NSTextView when creating it in Interface Builder)
The contentInsets were automatically being set to account for the invisible titlebar, even though the docs say that NSScrollView - automaticallyAdjustsContentInsets (which is set to YES by default and which I assume was causing this) doesn't do automatic insets for transparent titlebars.
After programmatically setting the scrollView's contentInsets to 0, everything works great!
In Objective C, you can set your scrollView's contentInsets to 0 like this:
NSScrollView *scrollView = (NSScrollView *)self.textView.superview.superview;
scrollView.automaticallyAdjustsContentInsets = NO; // Doesn't remove insets // Probably calling this too late
scrollView.contentInsets = NSEdgeInsetsMake(0, 0, 0, 0);
Here's a working example.
Hope this helps!
good morning :)
i have a problem with my datepicker in ios 9 swift 2
this is how my datepicker looks like in ios 8 swift 2 [OK]:
and this is my datepicker in ios 9 swift 2 [NOT OK]:
Any ideas how i can solve it?
I had similar issue with UILabel and UIDatePicker in same cell.
I figure out that setting witch for UIDatePicker based on container view width. Will cause this error, for ex:
Leading and Tailing to container view
Equal width as container view
But when you set width as:
Given width ex. 400
Equal width as UILable above
I hope this will help someone.
PS. I've set gray background for better look. First screen shot shows that UIDatePicker have some issues with rendering. I'm only guessing.
I had same issue and was able to resolve it by adding datepicker using this code:
UIDatePicker* picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 162)];
[self.view addSubview:picker];
I faced similar problem due to pinning Date Picker to container height & width, solution which worked for me was to also add constraint for width and & height from storyboard auto layout as below -
AppsWise
I hade the same issue while working with a picker within another cell.
Simply adding constraints worked like a charm for me.
Why i need to add constraints to a picker in swift 2 i still don't know.
Hope this helps.
I had a similar issue when adding a UIDatePicker programmatically into a complex UIView heirarchy. I just ended up using brute force, like so:
let datePicker = UIDatePicker()
datePicker.setDate(NSDate(), animated: false)
datePicker.datePickerMode = .Date
var tempFrame = datePicker.frame
let originalDatePickerWidth = tempFrame.size.width
if originalDatePickerWidth != self.view.bounds.width
{
tempFrame.size.width = self.view.bounds.width
tempFrame.size.height *= (self.view.bounds.width / originalDatePickerWidth)
datePicker.frame = tempFrame
}
Probably a very trivial Swift question.
I've got a view on my storyboard with various UIelements (labels, images etc).
What I want to do is change the position of some of these elements through code (so not using the storyboard or autolayout features).
When I create my elements dynamically I can just type:
distanceLabel.frame = CGRectMake( 100, 200, 50, 50); // set new position exactly
or
distanceLabel.frame.origin = CGPoint(0,0)
However this doesn't work for elements on my storboard (I'm calling them through the IBOutlets I've created). Any simple trick / tip / hint to do this?
Many Thanks
You can use outlets to modify constraints. Set constant property by code to modify the layout.
I have a view that needs to have its frame manipulated programmatically - it's a kind of document view that wraps to its content which is then scrolled and zoomed around a superview by manipulating the frame origin. Autolayout fights with this at runtime.
Disabling autolayout completely seems a bit harsh because it could reasonably be used to handle layout for the other views. It seems like what I might want is some kind of "null constraint".
I had the same problem. But I have resolved it.
Yes, you can disable auto layout at runtime for a specific UIView, instead of disabling it for the whole xib or storyboard which is set by default in Xcode 4.3 and later.
Set translatesAutoresizingMaskIntoConstraints to YES, before you set the frame of your subview:
self.exampleView.translatesAutoresizingMaskIntoConstraints = YES;
self.exampleView.frame = CGRectMake(20, 20, 50, 50);
I had a similar issue where Autolayout was overriding some of my frame-setting at run time (I had a dynamic view that in some cases pushed a new view controller...pushing and then pressing Back would reset the initial view).
I got around this by putting my manipulation code in viewDidLayoutSubviews of my View Controller. This seems to get called after whatever constraint mojo gets called, but before viewDidAppear, so the user is none the wiser.
Perhaps just setting translatesAutoresizingMaskIntoConstraints to YES (and not adding additional constraints affecting that view) will let you set the frame without fighting the auto layout system.
In iOS 8 you can set an NSLayoutConstraint to be active or not. So if I'm using interface builder, I add all my constraints to an OutletCollection and then activate or deactivate using:
NSLayoutConstraint.deactivateConstraints(self.landscapeConstraintsPad)
NSLayoutConstraint.activateConstraints(self.portraitConstraintsPad)
The particular application I'm using it for here is having different constraints in portrait and landscape mode and I activate/deactivate based on the rotation of the device. It means I can create some complex layout changes all in interface builder for both orientations, and still use auto layout without the verbose auto layout code.
Or you can activate / deactivate using removeConstraints and addConstraints.
I don't know if this will help anyone else, but I wrote a category to make this convenient because I find myself doing this a lot.
UIView+DisableAutolayoutTemporarily.h
#import <UIKit/UIKit.h>
#interface UIView (DisableAutolayoutTemporarily)
// the view as a parameter is a convenience so we don't have to always
// guard against strong-reference cycles
- (void)resizeWithBlock:(void (^)(UIView *view))block;
#end
UIView+DisableAutolayoutTemporarily.m
#import "UIView+DisableAutoResizeTemporarily.h"
#implementation UIView (DisableAutoResizeTemporarily)
- (void)resizeWithBlock:(void (^)(UIView * view))block
{
UIView *superview = self.superview;
[self removeFromSuperview];
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
__weak UIView *weakSelf = self;
block(weakSelf);
[superview addSubview:self];
}
#end
I use it like this:
[cell.argumentLabel resizeWithBlock:^(UIView *view) {
[view setFrame:frame];
}];
Hope it helps.
You can set the translatesAutoresizingMaskIntoConstraints type Boolean, Value Yes in the User Defined Runtime Attributes of the UIView you want in the xib/storyboard.
In my view I had a Label and a Text. The label had pan gesture. The label moves around fine during drag. But when I use the text box keyboard, the label resets its position to the original location defined in auto layout. The issue got resolved when I added the following in swift for the label. I added this in viewWillAppear but it can be added pretty much anywhere you have access to the target field.
self.captionUILabel.translatesAutoresizingMaskIntoConstraints = true
Open project in 4.5
Select storyboard
Open the file inspector
Under Interface Builder Document uncheck 'Use Autolayout'
You can split across multiple storyboards if you want to use autolayout for some views.
For me it worked to create the subview programmatically, in my case the auto layout was messing with a view that I needed to rotate around its center but once I created this view programmatically it worked.
I've encountered a similar scenario, where I joined a project that was initiated with auto-layout, but I needed to make dynamic adjustments to several views. Here is what has worked for me:
Do NOT have views or components laid out in interface builder.
Add your views purely programmatically starting with alloc/init and setting their frames appropriately.
Done.
This happened to me in a project without storyboards or xib files. All 100% code. I had an ad banner at the bottom and wanted the view bounds to stop at the ad banner. The view would resize itself automatically after loading. I tried every resolution on this page but none of them worked.
I ended up just creating a sub view with the shortened height and placed that in into the main view of the controller. Then all my content went inside the sub view. That solved the problem very easily without doing anything that felt like it was going against the grain.
I am thinking if you want a view that is not the normal size that fills the window then you should use a sub view for that.
Instead of disabling autolayout, I would just calculate the new constraint with the frame you are replacing. That appears to me to be the appropriate way. If you are adjusting components that rely on constraints, adjust them accordingly.
For example, if you have a vertical constraint of 0 between two views (myView and otherView), and you have a pan gesture or something that adjusts the height of myView then you can recalculate the constraint with the adjusted values.
self.verticalConstraint.constant = newMyViewYOriginValue - (self.otherView.frame.origin.y + self.otherView.frame.size.height);
[self.myView needsUpdateConstraints];
For those of you who are using auto layout, please check out my solution here. You should be making #IBOutlet's of the constraints you want to adjust and then change their constants.
if it's xib file:
select the .xib file
select the "File's Owner"
show the Utilities
click on: "File Inspector"
Under "Interface Builder Document" disable: "Use Autolayout"
I wanted to make the UIPageControl area (dots) shorter. I tried to change its height smaller in the Size Inspector but its height value is greyed out and it cant be changed. It's value by default is 36. Is there any way of decreasing this value programmatically?
I know that you can make the page controllers background see through, but I don't want that. I wanted to make the background slightly transparent, I did that, but I want the Height of the background to be smaller.
If someone can help me out then that would be awesome.
THANKS!
This is how you change the height of a UIPageControl. In the PageViewController.m in the viewDidLoad method, put the following code:
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100);
This updates the frame property of the view of your UIPageControl.
Try this:
CGRect frame = pageControl.frame;
frame.size.height = 10.0; // or whatever you want
pageControl.frame = frame;
This, of course, goes in your code, presumably in the controller that has access to your pageControl IBOutlet.