I have a simple to do list using a uitable inside a top bar and I want to create a overlay menu activated from one of the buttons on the top bar.
How can I set that menu uiview on top overlay of the uitable custom cell?
I am using Swift in the latest Xcode for ios8.
I am using the story board to try to insert the uiview or a ui container to set my menu. The idea is to initialise this view hidden and then a button on the top bar to show it. And this menu should have some uibuttons that I then want to create segways to different views. But I can't seem to be able to drag the view on top of the uitable in the storyboard. Is there a way? Even if using code?
Thanks.
You could programaticaly create a UIView:
var view2 = UIView(frame: CGRect(x: xPos, y: yPos, width: WIDTH, height: HEIGHT))
if you wan't to add buttons to the view you can do that:
var button = UIButton(frame: CGRect(x: xPos, y: yPos, width: WIDTH, height: HEIGHT))
button.titleLabel?.text = "ButtonTitle"
button.addTarget(self, action: Selector("functionToCallWhenClicked"), forControlEvents: UIControlEvents.TouchUpInside) //To do something when the button is tapped
self.view2.addSubview(button)//To add the button the the view2
And then you add view2 to the screen:
self.view.addSubview(view2)
If you wan't to remove the view2 from the screen:
self.view2.removeFromSuperview()
==> here Given Code Try iy
===>declare awView
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
awView.center = CGPointMake(self.view.frame.size.width/2,
self.view.frame.size.height-kAdWhirlViewHeight/2);
[self.view addSubview:awView];
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect newFrame = awView.frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.tableView.contentOffset.y+(self.tableView.frame.size.height-kAdWhirlViewHeight);
awView.frame = newFrame;
}
Related
I want to make a scroll view programmatically in xcode and want to add anchor constraints using safe area layout guide Auto Layout. And want to add some text views button and map init but could not find any proper way to do this. I have tried many codes. What is the proper code for this?
Please try below code for programmatically create Scroll view and add UIView inside XCode
Swift 4.0
import UIKit
class ViewController: UIViewController {
let scrollView: UIScrollView = {
let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let myView: UIView = {
let view = UIView()
view.backgroundColor = .yellow
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the scroll view to self.view
self.view.addSubview(scrollView)
// constrain the scroll view to 8-pts on each side
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8.0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8.0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
//Frame for UIView here
myView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
scrollView.addSubview(myView)
}
}
At first glance they look like regular UIButtons however they got a label below it. Also the background of the button seems to be a blurred effect.
So my thoughts are that they are put in a CollectionView (Horizontal). With each cell containing a UIButton and a UILabel. Although that may work the UIButton doesn't seem to get the move effect for free.
Is that custom behavior? And if so, how are you able to create such an effect?
I bet it is not an UICollectionView but a horizontal UIStackView of custom views in which there is a UIButton and UILabel vertically aligned.
Here you have an example, using stackViews:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .equalSpacing
stackView.alignment = .center
stackView.spacing = 30
view.addSubview(stackView)
["One", "Two", "Three", "Caramba"].forEach {
let buttonStackView = UIStackView()
buttonStackView.axis = .vertical
buttonStackView.distribution = .fillProportionally
buttonStackView.alignment = .center
buttonStackView.spacing = 15
let button = UIButton(type: .system)
button.setTitle($0, for: .normal)
buttonStackView.addArrangedSubview(button)
let label = UILabel()
label.text = $0
label.font = UIFont.systemFont(ofSize: 20)
buttonStackView.addArrangedSubview(label)
stackView.addArrangedSubview(buttonStackView)
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
Having a custom view instead of a vertical uistackview for each button would allow to customize its layout when focused, including Parallax effect.
For adding parallax effect to each button in the stack, take a look to How to get Parallax Effect on UIButton in tvOS?
In a UITableView like below... is there an easy way to move the checkmark buttons from the left side of the UITableViewCell to the right side?
Yes you can do this using UITableViewCell's accessoryView like below:
cell.accessoryType = .Checkmark
cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20)) //You can change to UIButton with your own custom checkmark button
cell.accessoryView.backgroundColor = UIColor.blueColor() //change to your color
I have a UIScrollView inside a UIViewController (subclassed by ImageViewController). The ViewController itself is part of a NavigationController's stack. Now, apart from having a navigation bar, I want the ScrollView to take all of the available room on the screen. The UIImageView inside the scrollview should then fill the available room of the scroll view. You can see the current state at the bottom of this posting.
class ImageViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
var imageView: UIImageView?
var image: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
if let image = image {
imageView = UIImageView(image: image)
if let imageView = imageView {
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
scrollView.addSubview(imageView)
scrollView.contentSize = image.size
let scaleHeight = scrollView.frame.size.height / scrollView.contentSize.height
let scaleWidth = scrollView.frame.size.width / scrollView.contentSize.width
let minimumScale:CGFloat = min(scaleHeight, scaleWidth)
let maximumScale:CGFloat = max(scaleHeight, scaleWidth)
scrollView.minimumZoomScale = minimumScale
scrollView.maximumZoomScale = maximumScale
scrollView.zoomScale = maximumScale
}
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}
The code leaves me with unnecessary borders (left, right, top). How do I get rid of them?
EDIT: With #Bxtr's suggestion and another stackoverflow thread I was able to remove the borders left and right to the scroll view. After some more digging I found out that by deactivating Adjust Scroll View Insets, the image inside the scroll view can be correctly vertically positioned. Still, I do not get the reason for the vertical misplacement in the first place...
Have you checked the margin/padding values, because it kinda looks so (same size on left and right border). If it is not the case, could you please also post your xml file of the activity so we can have every part of the puzzle to help you ?
scrollView.contentSize = image.size;
you have to tweek this line. You are explicitly setting scroll view content size to the image size. You have to set content size to fit the Width of Screen.
You can use a UIView in UIScrollView, and that UIView contains UIImage.
You need to set constraints properly.
After some more digging I found out that by deactivating Adjust Scroll
View Insets, the image inside the scroll view can be correctly
vertically positioned. Still, I do not get the reason for the vertical
misplacement in the first place...
The reason is that the view controller's automaticallyAdjustsScrollViewInsets property is by default YES, the following is from apple documentation:
automaticallyAdjustsScrollViewInsets
A Boolean value that indicates
whether the view controller should automatically adjust its scroll
view insets.
Default value is YES, which allows the view controller to adjust its
scroll view insets in response to the screen areas consumed by the
status bar, navigation bar, and toolbar or tab bar. Set to NO if you
want to manage scroll view inset adjustments yourself, such as when
there is more than one scroll view in the view hierarchy.
Besides setting automaticallyAdjustsScrollViewInsets = No, you can pin the scrollView to the topLayoutGuide (instead of to the top of the viewController's view) when using autoLayout.
I have a tableView and want to add a toolbar at the bottom with a sync button and the latest sync date (like on the Mail-app on the iPhone). But the Scrollbar should be fixed, so that it doesn't move when I scroll trough the table view.
Right now, I'm just adding the searchBar programmatically by:
self.tableView.tableFooterView = self.toolbar;
Another problem linked here is the fact, that I'm using the MasterDetailsView Controller template of Xcode. But I can't just drag a toolbar into my MasterView (containing the tableview). My first guess is that its just fixed because of the template I used but I'm not quite sure about it.
Thanks in advance!!
Instead of using a UITableViewController try using a plain UIViewController which conforms to UITableViewDelegate and UITableViewDataSource. Add a UITableView as a subview to the view controller's main view and link its delegate and datasource to the view controller.
Now, you can add any other UIViews to the main view which will appear fixed on top of the UITableView.
In your Story board for that ViewController, drag Bar Button Item to the Bottom after the tableView and in viewDidLoad method.. do the following..
self.navigationController.toolbarHidden = NO;
I think I found a really good solution to this.
In the storyboard view, drop the toolbar out of the Tableview. In the story board view this will leave it 'floating' above the view and the tool bar will no longer appear in your compiled code.
Too fix this, add the following to your view controller code
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let toolBarView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
mainToolBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)
toolBarView.addSubview(mainToolBar)
v.addSubview(toolBarView)
return v
}